简体   繁体   中英

Method not firing on textbox text change

I have a textbox with a calander extender attached:

<asp:TextBox ID="customDateTo" runat="server" AutoPostBack="true" OnSelectionChanged="customDateFrom_SelectionChanged"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="toCalendarExtender" TargetControlID="customDateTo" runat="server"></ajaxToolkit:CalendarExtender>

As you see I have AutoPostBack="true" and OnSelectionChanged="customDateFrom_SelectionChanged assigned for the textbox.

However nothing is responding in my method:

protected void customDateFrom_SelectionChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Do Something");
}

When I change the text I do get a postback but nothing within the method is executing. Why is this happening and how do I fix it?

For a TextBox , I think it should be;

OnTextChanged="customDateFrom_SelectionChanged"

Not

OnSelectionChanged="customDateFrom_SelectionChanged"

I assume you want to handle the TextBox ' TextChanged event instead. SelectionChanged is a winforms event which doesn't exist in ASP.NET.

<asp:TextBox ID="customDateTo" runat="server" 
   AutoPostBack="true" OnTextChanged="customDateFrom_TextChanged">
</asp:TextBox>

This event is raised when the user changes the text in it and leaves the TextBox .

protected void customDateFrom_TextChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Do Something");
}

Another reason for not triggering an event is if you databind the TextBox before the event is triggered (fe in Page_Load ). Then you should check IsPostBack : if(!IsPostBack)DataBindTextBox(); .

You should handle the TextBox.TextChanged Event ..

example:

 <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" 
            ontextchanged="TextBox1_TextChanged">

// server side event:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   Label1.Text = Server.HtmlEncode(TextBox1.Text);
}

Refer: Asp.Net textbox TextChanged event is not firing

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