Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created 10 dynamic textboxes..1st is for username and 2nd is for password and so on .so i want to set watermark for each textbox..so user should know which textbox is for username and which is for password..plz help me...

//code to create dynamic textbox

C#
int count=(int)ViewState["textboxcount"];
            count++;

            for (int i = 0; i <= count; i++)
            {
                ViewState["textboxcount"] = (int.Parse(ViewState["textboxcount"].ToString()) + 1).ToString();
                //Response.Write("Last Control ID" + ButtonName + Environment.NewLine);
                TextBox TxtATUserID = new TextBox();
                TextBox TxtATPassword = new TextBox();
                TxtATUserID.ID = "TxtUserName" + ViewState["textboxcount"].ToString();
                // TxtATUserID.Text = TxtATUserID.ID.ToString();
                TxtATPassword.ID = "TxtPassword" + ViewState["textboxcount"].ToString();
                // TxtATPassword.Text = TxtATPassword.ID.ToString();

                TxtATPassword.TextMode = TextBoxMode.Password;
                this.PlaceHolder1.Controls.Add(TxtATUserID);
                this.PlaceHolder1.Controls.Add(TxtATPassword);
Posted
Updated 28-May-13 20:17pm
v2
Comments
vijay__p 29-May-13 2:19am    
You can add TextBoxWatermarkExtender alogn with Textbox on page.
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/TextBoxWatermark/TextBoxWatermark.aspx
Sergey Alexandrovich Kryukov 29-May-13 2:31am    
It looks like OP needs exactly this, not "real watermarks". Will you post it as an answer? I would up-voted it as the correct answer.
—SA
Sergey Alexandrovich Kryukov 29-May-13 2:29am    
Yes, we can. How about your? :-)
This is not really a question. Help with what, exactly? What did you try? You code sample is not related to watermarks.
—SA
Member 9579525 29-May-13 2:31am    
can we use TextBoxWatermarkExtender for dynamically created texboxes??
Sergey Alexandrovich Kryukov 29-May-13 2:32am    
I guess the first comment provides you with a solution. Did you check it out?
—SA

1 solution

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.NET
<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.
  1. Add Text to TextBoxes.
  2. Add Gray Color.
  3. Add the Attributes.


C#
TextBox TxtATUserID = new TextBox();
TextBox TxtATPassword = new TextBox();

// Add text to TextBoxes.
TxtATUserID.Text = "User ID";
TxtATPassword.Text = "Password";

// Add Gray color.
TxtATUserID.ForeColor = System.Drawing.Color.Gray;
TxtATPassword.ForeColor = System.Drawing.Color.Gray;

// Add the Attributes.
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.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 29-May-13 8:45am    
Yes, nice and clear, it looks like. My 5.
—SA
Thanks a lot Sergey Alexandrovich Kryukov... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900