Prev: Retrieving Property Values without Loading Referenced Assemblies
Next: ListView: Text from previous row
From: Rnes on 14 Apr 2010 10:08 I have a textbox defined .. <asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></ asp:TextBox> So it will fire off the tbSubmitterId.TextChanged event. Here is the problem, 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?
From: Andrew Morton on 14 Apr 2010 10:54 Rnes wrote: > I have a textbox defined .. > > <asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></ > asp:TextBox> > > So it will fire off the tbSubmitterId.TextChanged event. > > Here is the problem, > > 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? A simple way is to get both events to call another method which does what you want: sub someAction() ' do the required action here end sub sub textBoxChangedHandler() someAction() end sub sub buttonClickedHandler() someAction() end sub HTH, Andrew
From: Gregory A. Beamer on 14 Apr 2010 11:09 "Rnes" <nesr235(a)lni.wa.gov> wrote in message news:2ec2835b-ca4c-4691-9bac-5333b7fd48b9(a)e7g2000yqf.googlegroups.com... > I have a textbox defined .. > > <asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></ > asp:TextBox> > > So it will fire off the tbSubmitterId.TextChanged event. > > Here is the problem, > > 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! | ************************************************
From: Mr. Arnold on 14 Apr 2010 11:27 Rnes wrote: > I have a textbox defined .. > > <asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></ > asp:TextBox> > > So it will fire off the tbSubmitterId.TextChanged event. > > Here is the problem, > > 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? You remove the AutoPostBack off of the Textbox. You get the TextBox.Text at the Submit button click event and do whatever with the text value before you do anything else in the button click event. You get rid of the Textchanged event so only one event fires, the button click event.
From: Rnes on 14 Apr 2010 14:15 On Apr 14, 8:09 am, "Gregory A. Beamer" <NoSpamMgbwo...(a)comcast.netNoSpamM> wrote: > "Rnes" <nesr...(a)lni.wa.gov> wrote in message > > news:2ec2835b-ca4c-4691-9bac-5333b7fd48b9(a)e7g2000yqf.googlegroups.com... > > > > > > > I have a textbox defined .. > > > <asp:TextBox ID="tbSubmitterId" runat="server" AutoPostBack="True"></ > > asp:TextBox> > > > So it will fire off the tbSubmitterId.TextChanged event. > > > Here is the problem, > > > 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 - Thanks Greg. FYI, I am using ASP.net 2.0 I need the autopostback on the text box, because it does a lookup to validate the code that was entered there. Most of the time the user enters the data, then either hits TAB to go to the next field or clicks on another part of the page to do something and then clicks on the button. I tried as you suggested (but with vb) ... If Request.Form("BtnGenerateList") = "Generate List" Then Handle_GenerateList_Click() End If The problem is that since the event for the textbox change is fired then "Request.Form("BtnGenerateList") " shows as nothing. But, if just the button is clicked then the statment "If Request.Form("BtnGenerateList") = "Generate List" " becomes true.
|
Next
|
Last
Pages: 1 2 Prev: Retrieving Property Values without Loading Referenced Assemblies Next: ListView: Text from previous row |