C#/ASP.net

asp:textbox에 placeholder 입력값 예제 만들기

saltdoll 2018. 5. 8. 08:20
반응형

asp 페이지의 asp:textbox 에 힌트로 보여질 html5에서 사용되는 placeholder 에 해당하는 문자 나오게 하기.

컨트롤러에 placeholder 애튜리뷰트를 추가하기. (아래의 방법이 가장 간단한 방법)

<asp:textbox id="txtWithHint" placeholder="hint" runat="server"/>


<input type="text" placeholder="hint"/>


그외의 방법들도 소개합니다.

 

 

The placeholder attribute

You're looking for the placeholder attribute. Use it like any other attribute inside your ASP.net control:

<asp:textbox id="txtWithHint" placeholder="hint" runat="server"/>

Don't bother about your IDE (i.e. Visual Studio) maybe not knowing the attribute. Attributes which are not registered with ASP.net are passed through and rendered as is. So the above code (basically) renders to:

<input type="text" placeholder="hint"/>

Using placeholder in resources

A fine way of applying the hint to the control is using resources. This way you may have localized hints. Let's say you have an index.aspx file, your App_LocalResources/index.aspx.resx file contains

<data name="WithHint.placeholder">
    <value>hint</value>
</data>

and your control looks like

<asp:textbox id="txtWithHint" meta:resourcekey="WithHint" runat="server"/>

the rendered result will look the same as the one in the chapter above.

Add attribute in code behind

Like any other attribute you can add the placeholder to the AttributeCollection:

txtWithHint.Attributes.Add("placeholder", "hint");


 

 

참고: https://stackoverflow.com/questions/15823983/how-do-i-put-hint-in-a-asptextbox

반응형