Reputation: 3
I have built an editing page. There is a comments section and the possibility exists that the comments may need to be removed.
I have committed to using TextChanged
as the users were worried about clicking buttons (rolls eyes). I'm creating the event dynamically like this:
txtComments.TextChanged += UpdateComments;
The event works fine if there is anything in the textbox but if all text is removed then the event doesn't fire.
any help would be appreciated.
Upvotes: 0
Views: 1694
Reputation: 723
This is quite baffling, I can't seem to reproduce your problem. Here is the code I've used to get it to work, perhaps you can compare the code to what I've done and see if there is any difference.
protected void Page_Load(object sender, EventArgs e)
{
var txt = new TextBox
{
Text = "Initial Text"
};
txt.TextChanged += ThisTextBoxChanged;
form1.Controls.Add(txt);
}
private static void ThisTextBoxChanged(object sender, EventArgs e)
{
System.Diagnostics.Debugger.Break();
}
Upvotes: 0
Reputation: 680
Since your element is being created dynamically, then make sure that you set event handler on each post back. I believe you must set it on Pre_Init vs Page_Load but I have not dealt with the ASP.NET page life cycle in a while. Here is a link: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx
Upvotes: 0
Reputation: 723
The method will always run if the value of the textbox has changed since the last post to the server. It will compare the value currently to the one that was last sent to the server and decide if the method should be invoked.
Upvotes: 0
Reputation: 3314
From the MSDN of the System.Web.UI.WebControls.TextBox control:
The TextChanged event is raised when the content of the text box changes between posts to the server. The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.
Clearing the TextBox
between posts with code will cause the behavior you are describing (no event firing).
Upvotes: 1