|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
tie ENTER to a button?Hi;
I have a page with several submit buttons on it. Is there a way to tie TextBox controls to specific buttons so if the user hits the ENTER button while in a text box, it is the submit button tied to it? -- thanks - dave david_at_windward_dot_net http://www.windwardreports.com Cubicle Wars - http://www.windwardreports.com/film.htm Hi Dave,
The form tag has an optional attribute "defaultbutton" which could specify which button to submit when enter is pressed. However, it will only use one default submit button on the whole form. I understand that you purpose is to use different default submit button when focused in different input fields. One possible workaround is to handle each fields' client-side event onkeypress and check the key there. ASP.NET 2.0 has an embedded javascript source which has a function called WebForm_FireDefaultButton() could do that: <form id="form1" runat="server"> <div> <asp:TextBox ID="text1" runat="server"> </asp:TextBox> <asp:TextBox ID="text2" runat="server"></asp:TextBox> <asp:Button ID="button1" runat="server" Text="button1" OnClick="button1_Click" /> <asp:Button ID="button2" runat="server" Text="button2" OnClick="button2_Click"/> </div> </form> protected void Page_Load(object sender, EventArgs e) { ClientScript.RegisterClientScriptResource(typeof(Page), "WebForms.js"); text1.Attributes.Add("onkeypress", "javascript:return WebForm_FireDefaultButton(event, 'button1')"); text2.Attributes.Add("onkeypress", "javascript:return WebForm_FireDefaultButton(event, 'button2')"); } protected void button1_Click(object sender, EventArgs e) { Response.Write("button1"); } protected void button2_Click(object sender, EventArgs e) { Response.Write("button2"); } Warning: this is using some undocumented javascript resource and function in ASP.NET 2.0. Regards, Walter Wang (waw***@online.microsoft.com, remove 'online.') Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. ok - I'll play around with this.
-- Show quoteHide quotethanks - dave david_at_windward_dot_net http://www.windwardreports.com Cubicle Wars - http://www.windwardreports.com/film.htm "Walter Wang [MSFT]" wrote: > Hi Dave, > > The form tag has an optional attribute "defaultbutton" which could specify > which button to submit when enter is pressed. However, it will only use one > default submit button on the whole form. I understand that you purpose is > to use different default submit button when focused in different input > fields. > > One possible workaround is to handle each fields' client-side event > onkeypress and check the key there. ASP.NET 2.0 has an embedded javascript > source which has a function called WebForm_FireDefaultButton() could do > that: > > <form id="form1" runat="server"> > <div> > <asp:TextBox ID="text1" runat="server"> > </asp:TextBox> > <asp:TextBox ID="text2" runat="server"></asp:TextBox> > <asp:Button ID="button1" runat="server" Text="button1" > OnClick="button1_Click" /> > <asp:Button ID="button2" runat="server" Text="button2" > OnClick="button2_Click"/> > </div> > </form> > > > protected void Page_Load(object sender, EventArgs e) > { > ClientScript.RegisterClientScriptResource(typeof(Page), > "WebForms.js"); > text1.Attributes.Add("onkeypress", "javascript:return > WebForm_FireDefaultButton(event, 'button1')"); > text2.Attributes.Add("onkeypress", "javascript:return > WebForm_FireDefaultButton(event, 'button2')"); > } > protected void button1_Click(object sender, EventArgs e) > { > Response.Write("button1"); > } > protected void button2_Click(object sender, EventArgs e) > { > Response.Write("button2"); > } > > > Warning: this is using some undocumented javascript resource and function > in ASP.NET 2.0. > > Regards, > Walter Wang (waw***@online.microsoft.com, remove 'online.') > Microsoft Online Community Support > > ================================================== > When responding to posts, please "Reply to Group" via your newsreader so > that others may learn and benefit from your issue. > ================================================== > > This posting is provided "AS IS" with no warranties, and confers no rights. > >
Global resources not localized on IIS
How to know the selected row in a DataControlField Validation Groups and Wizard step DetailsView Default Mode Why does OnClick event not work in 2.0 for DataGrid site maps & menu control Customising the CatalogZone class Why doesn't work DataFormatString for GridView.BoundField ? Checkboxlist Question web control with GDI+ and MouseMovements. |
|||||||||||||||||||||||