Search code examples
c#asp.nettextboxtextchanged

when click on empty asp:textbox I want to set the visibility of a asp:label to false immediately


when click on empty asp:textbox I want to set the visibility of a asp:label to false immediately i set some label's to validation check when the textbox is empty , the label become visible but when text changed or clicked on textbox to edit it is not invisibled please help me

code :

 protected void Button1_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "")
        Label1.Visible = true;

}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Label1.Visible = false;

}

Solution

  • Make sure you have kept AutoPostBack="true" on TextBox1

    protected void Button1_Click(object sender, EventArgs e)
    {
            if (TextBox1.Text == "")
                Label1.Visible = true;
    
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
            if(TextBox1.Text != string.empty)
            {
                Label1.Visible = false;
            }
    
    }
    

    But there are other efficient ways to validate the same. eg. ASP Validators, JQuery Validators