Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method not firing on textbox text change

Tags:

c#

asp.net

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?

like image 396
David Tunnell Avatar asked Dec 02 '25 04:12

David Tunnell


2 Answers

For a TextBox, I think it should be;

OnTextChanged="customDateFrom_SelectionChanged"

Not

OnSelectionChanged="customDateFrom_SelectionChanged"
like image 127
Kaf Avatar answered Dec 04 '25 19:12

Kaf


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 (f.e. in Page_Load). Then you should check IsPostBack: if(!IsPostBack)DataBindTextBox();.

like image 42
Tim Schmelter Avatar answered Dec 04 '25 18:12

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!