简体   繁体   English

TextBox_TextChanged事件未在C#中触发

[英]TextBox_TextChanged Event not firing in c#

The DOB_TextChanged event not firing.My code is, DOB_TextChanged事件未触发。我的代码是,

Aspx code Aspx代码

<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 . 您必须检查一件事。. 转到 aspx (设计)页面 Select the button. 选择按钮。 and click F4(Properties). 并单击F4(属性)。 There you have to go Event area. 那里您必须去活动区。 Here, Check if the TextChanged event is given correctly, like in the server page. 在这里,检查是否正确给出了TextChanged事件,例如在服务器页面中。

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. 设置文本框属性AutoPostBack = True,它仅在更改文本后控件退出TextBox时才起作用。如果文本相同,则不会引发该事件。

You can use LinkButton and OnRowCommand instead: 您可以改用LinkButtonOnRowCommand

Aspx 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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