You must Sign In to post a response.
  • Category: ASP.NET

    How to clear the label using html change event

    My code as follows

    <input type = "text" id="Empid" Placeholder = "Hello" runat="server">

    <asp:label id="lblmsg" runat="server">


    In my run mode as follows


    Hello (Input type)

    lblmsg The data does not match

    When i type the text in Input type, i want to clear the lblmsg text the data does not match.


    for that how can i do in asp.net
  • #767830
    Only HTML is unable to complete your task you need to use javascrpt for that, there is no onchnage event for input type in html you need to use either onKeyUp or onKeyDown method. see below snippet for more details
    to change label text you need to use InnetHTML property of label

    < script type="text/javascript">
    function bar() {
    document.getElementById("lbl1").innerHTML = 'Label text changed';
    }
    < input type="text" name="foo" onKeyUp="return bar()" />


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767838
    Hi

    Using Html textbox and Html label you can achieve by writing the below code.

    <head runat="server">
    <title></title>
    <script type="text/javascript">

    function myfunction() {
    debugger;
    var name = document.getElementById("txtname").value;
    if(name=="")
    {
    document.getElementById("lblmesg").innerHTML = "data doesn't match";
    }
    else
    {
    document.getElementById("lblmesg").innerHTML = "";
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txtname" placeholder="hello" onblur ="myfunction();" />
    <label id="lblmesg"></label>
    </div>
    </form>
    </body>


    If you want to take asp textbox and asp label you can achieve by writing below code.

    <asp:TextBox ID="txtname2" runat="server" Text="Hello" AutoPostBack="true" OnTextChanged="txtname2_TextChanged"></asp:TextBox>
    <asp:Label ID="lblmesg2" runat="server"></asp:Label>

    In the .cs file you need to specify below code under textchanged event of textbox

    protected void txtname2_TextChanged(object sender, EventArgs e)
    {
    string name = txtname2.Text;
    if (name == string.Empty)
    {
    lblmesg2.Text = "data doesn't match";
    }
    else
    {
    lblmesg2.Text = "";
    }
    }


    Find the attached screen shot.

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment


  • Sign In to post your comments