I have a form in which i have a text box for email and many other textboxes.
The email has a functionality that it should not match with previously entered email but it can be blank.
I fired my code at the text change event of the textbox and it is working fine.
But when i click on the update button without taking my cursor off the textbox.
The click of the button is not working.Only textchanged event of textbox fires nothing else happen.
Please help me with the same.
string emailid = txtemail.Text;
var emailcnt2 = (from r in context.Customers
where r.Email == Convert.ToString(emailid)
select r).SingleOrDefault();
if (emailcnt2 != null)
{
lblemail.Text = "Email already exists for customer " + emailcnt2.FirstName + " " + emailcnt2.LastName + "";
// emaildisplay.Attributes.Add("style", "display:block;");
txtemail.Focus();
}
I looked on project where I had this issue and the “hack” was to put the textbox inside an
UpdatePanel, it seems it works in most of the cases ( I had tests when the button click didn’t fire, but they were few, actually very rare).I can’t tell exactly why it works, I don’t have a certain or valid explanation, but probably is because the UpdatePanel makes its own request, while the form submit it’s just another request.
Going further, I’m trying to think to an explanation of this behavior. It seems the TextChanged event is really firing when the text is changed (hilarious, no?). We expect this event to fire at onblur moment, if it’s the case, ie the text changed, but when some other button is clicked, probably the javascript generated by ASP .Net checks if the text is changed and then submits the form to fire this event. The button click is lost now, because a form cannot be submitted twice.
Now, what UpdatePanel brings is that it has the responsibility to fire the TextChanged event, so the button click is now fired as usual.