简体   繁体   中英

TextBox_TextChanged Event not firing in c#

The DOB_TextChanged event not firing.My code is,

Aspx code

<asp:TemplateField HeaderText="DOB">
<EditItemTemplate>
  <asp:TextBox ID="DOB" CssClass="datepick" runat="server" AutoPostBack="true" 
               OnTextChanged="DOB_TextChanged"></asp:TextBox>
</EditItemTemplate>
 </asp:TemplateField>

code behind,

protected void DOB_TextChanged(object sender, EventArgs e)
{
    //mycode
}

am i did anything wrong??

You have to check one thing.. Go to aspx (Design) page . Select the button. and click F4(Properties). There you have to go Event area. Here, Check if the TextChanged event is given correctly, like in the server page.

Just a simple code

 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <br />
    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    <br />

and

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("button_click event raised");
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
         Response.Write("text_changed event raised : "+ TextBox1.Text);
    }

Set the text box property AutoPostBack=True , and it will work when control goes out of TextBox only after change in text .If text is same than it will not raise the event.

You can use LinkButton and OnRowCommand instead:

Aspx

<EditItemTemplate>
    <asp:LinkButton ID="test" CommandName="YourCommandName" runat="server">Test</asp:LinkButton>

</EditItemTemplate>

CodeBehind:

protected void Item_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "YourCommandName")
    {
         //to do
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM