Home > ASP.Net, Javascript > Clear TextBox on focus on it

Clear TextBox on focus on it

Hi

try this example to clear TextBox value on Focus on it using Javascript

<asp:TextBox ID="TextBox1" runat="server" Height="43px" Width="280px"></asp:TextBox>

    protected void Page_Load(object sender, EventArgs e)
    {
           TextBox1.Attributes.Add("onfocus", "this.value=''");
    }

also you can try this :-

private void ClearTxtMyboxScript()
    {
        TextBox1.Attributes.Add("onFocus", "ClearTextbox()");
        StringBuilder sb = new StringBuilder();
        sb.Append("<script language=\"javascript\">\n");
        sb.Append("function ClearTextbox() {\n");
        sb.Append("eval(\"document.forms[0].TextBox1.value = \\'\\'\");\n");
        sb.Append("}\n");
        sb.Append("</script>");
        Page.RegisterClientScriptBlock("ClearTextbox", sb.ToString());
    }

protected void Page_Load(object sender, EventArgs e)
    {
        ClearTxtMyboxScript();
    }

Hope this helps

Good Luck

About these ads
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: