You need to add some attribute to the
TextBox
at runtime so that it can be
WaterMarked
.
Refer -
Asp.net create watermark text for textbox in javascript[
^], which the simplest method to do the
WaterMarking
on
TextBox
. But the
TextBox
is not dynamic.
<asp:Textbox ID="txtUserName"
runat="server"
Text="Enter Username Here"
ForeColor="Gray"
onblur="WaterMark(this, event);"
onfocus="WaterMark(this, event);" />
So, here there are different attributes defined to do this task.
As you have dynamic
TextBox
, so you have to just add those dynamically from code behind.
Use the
javaScript
functions defined there and add the following codes to add attributes dynamically.
So, you need to 3 steps.
- Add
Text
to TextBoxes
. - Add
Gray
Color
. - Add the Attributes.
TextBox TxtATUserID = new TextBox();
TextBox TxtATPassword = new TextBox();
TxtATUserID.Text = "User ID";
TxtATPassword.Text = "Password";
TxtATUserID.ForeColor = System.Drawing.Color.Gray;
TxtATPassword.ForeColor = System.Drawing.Color.Gray;
TxtATUserID.Attributes.Add("onblur", "javascript:WaterMark(this, event);");
TxtATUserID.Attributes.Add("onfocus", "javascript:WaterMark(this, event);");
TxtATPassword.Attributes.Add("onblur", "javascript:WaterMark(this, event);");
TxtATPassword.Attributes.Add("onfocus", "javascript:WaterMark(this, event);");
TxtATUserID.ID = "TxtUserName" + ViewState["textboxcount"].ToString();
TxtATPassword.ID = "TxtPassword" + ViewState["textboxcount"].ToString();
TxtATPassword.TextMode = TextBoxMode.Password;
this.PlaceHolder1.Controls.Add(TxtATUserID);
this.PlaceHolder1.Controls.Add(TxtATPassword);
NOTE
Don't forget to add that
javaScript
function
WaterMark()
in your page.