Asp.net: add placeholder text to textbox

In this article I am going to explain how to add placeholder text (example text) to textbox in asp.net

Description:
We use the placeholder attributes in textbox to specify a short hint that describes the expected value. E.g. for email textbox we specify Enter Email e.g. abc@gmail.com.

Implementation:
Add a webform to project. Drag and drop the two textboxes from toolbox to webform.
HML Markup:
<fieldset style="width:300px">
    <legend>Watermark in Textbox</legend>
    <table>
    <tr>
    <td>Username</td> <td><asp:TextBox ID="txtusername" runat="server" placeholder="Enter Username" ToolTip="Enter Username"></asp:TextBox></td>
    </tr>
    <tr><td></td><td></td></tr>
     <tr>
    <td>Password</td> <td><asp:TextBox ID="txtpwd" runat="server" placeholder="Enter password" ToolTip="Enter password"></asp:TextBox></td>
    </tr>
    </table>
    </fieldset>

We can also add the placeholder text to textbox through code behind file:

C# Code:
protected void Page_Load(object sender, EventArgs e)
    {
txtusername.Attributes.Add("placeholder", "Enter Username");
txtpwd.Attributes.Add("placeholder", "Enter Password");
    }

VB.net Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
txtusername.Attributes.Add("placeholder", "Enter Username")
txtpwd.Attributes.Add("placeholder", "Enter Password")
End Sub



Post a Comment

2 Comments

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)