I have a textbox defined ..
<asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></
asp:TextBox>
So it will fire off the tbSubmitterId.TextChanged event.
1. the user enters a value in the textbox.
2. then they click the "Submitt" button (without moving out of the
textbox)
3. The "TextChanged" event is fired but the button event is not
fired. So basically the user would have to
click the button twice.
How do I tell in the "TextChanged" subroutine that the button was
clicked so I can process that event also?
First, I would personally get rid of the TextChanged event unless you are
doing something AJAX with it, as it causes confusion in the program flow.
You can decide whether that makes sense, but I see very few of these "every
control fires a server post" scenarios that are sane.
The answer to the problem at hand depends on what version of ASP.NET. The
rules are pretty much the same, however. You have to find the button in the
TextChanged event and call the action you want to perform if the button was
submitted. For example, in ASP.NET 4.0, both events would fire. I believe
this may be true in older versions, but I am fairly steeped in VS 2010 now.
Let's take a best practices approach:
1. The actual event handler calls routines and DOES NOT do the work
protected void SubmitButton_Click(object sender, EventArgs e)
{
//Don't do work here
SubmitForm();
}
private void SubmitForm()
{
//Do work here
}
2. In the text changed event, you need to test for the button submit. Here
is a fully qualified ASP.NET name version:
if(Request.Form["ctl00$MainContent$SubmitButton"] = "{Text on button}")
{
//Call form submit routine
}
But the naming might be more like this:
if(Request.Form["SubmitButton"] = "{Text on button}")
{
//Call form submit routine
}
So here is a TextChanged event:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
ChangeUserTextBox();
if (Request.Form["SubmitButton"] = "Submit")
{
SubmitForm();
}
}
In ASP.NET 4.0, you can alter naming declaratively to get to the more simple
syntax.
Now, this might still not work in some versions of ASP.NET (don't have any
info on this, but if the JavaScript does not capture the Submit button on
the form submit from text changed, you will capture nothing).
--
Peace and Grace,
Greg
Twitter: @gbworld
Blog:
http://gregorybeamer.spaces.live.com
************************************************
| Think outside the box! |
************************************************- Hide quoted text -
- Show quoted text -