When a web page has multiple buttons, you might want to change the default button in different cases. For example, imagine a web page that has a search area with datagrid results. The search area allows you to filter your search. When a result is selected to be edited, the fields that can be edited and a 'Save' button are displayed. If the 'Search' button is the default button, think about what happens when someone edits an item and hits 'Enter' while in an edit control field...'Search' is submitted, not 'Save'. Now you have to redo your changes and click 'Save'.
A lot of people post the following solution:
Page.RegisterHiddenField("__EVENTTARGET", "cmdSave") For some reason, this doesn't always work. Even when 'cmdSave' is set a the '__EVENTTARGET', the page is submitted but the button's event isn't fired.
To resolve this, we need to submit the form ourselves. The following snippet of code shows how to assign submit buttons to controls (TextBoxes, DropDownLists, etc.). Once you implement the main method into your page's base class, it's only one line of code per control to set the default button.
1. Enum of Control Types - This enumeration lists the control types you can set a default button for. Add more as needed.
1public enum ControlTypes
2{
3 None = 0,
4 TextBox = 1,
5 DropDownList = 2,
6 CheckBox = 3,
7 ListBox = 4
8}2. Method in Base Class to associate control to button 1public void SetDefaultButtonForControl(System.Web.UI.Page objPage,
2 Enums.ControlTypes intControlType, object objControl,
3 System.Web.UI.WebControls.Button objButton)
4{
5 string strScript = @"<SCRIPT language=""javascript"">
6 <!--
7 function catchKeyDown(btn, event)
8 {
9 if (document.all)
10 {
11 if (event.keyCode == 13)
12 {
13 event.returnValue=false;
14 event.cancel = true;
15 btn.click();
16 }
17 }
18 else if (document.getElementById)
19 {
20 if (event.which == 13)
21 {
22 event.returnValue=false;
23 event.cancel = true;
24 btn.click();
25 }
26 }
27 else if(document.layers)
28 {
29 if(event.which == 13)
30 {
31 event.returnValue=false;
32 event.cancel = true;
33 btn.click();
34 }
35 }
36 }
37 // -->
38 </SCRIPT>";
39 objPage.RegisterStartupScript("DefaultButtonForControl", strScript);
40
41 //Register with the control
42 switch (intControlType)
43 {
44 case Enums.ControlTypes.TextBox:
45 {
46 TextBox objTextBox = objControl as TextBox;
47 objTextBox.Attributes.Add("onkeydown",
"catchKeyDown(" + objButton.ClientID + ",event)");
48 break;
49 }
50 case Enums.ControlTypes.DropDownList:
51 {
52 DropDownList objDropDownList = objControl as DropDownList;
53 objDropDownList.Attributes.Add("onkeydown",
"catchKeyDown(" + objButton.ClientID + ",event)");
54 break;
55 }
56 case Enums.ControlTypes.ListBox:
57 {
58 ListBox objListBox = objControl as ListBox;
59 objListBox.Attributes.Add("onkeydown",
"catchKeyDown(" + objButton.ClientID + ",event)");
60 break;
61 }
62 case Enums.ControlTypes.CheckBox:
63 {
64 CheckBox objCheckBox = objControl as CheckBox;
65 objCheckBox.Attributes.Add("onkeydown",
"catchKeyDown(" + objButton.ClientID + ",event)");
66 break;
67 }
68 }
69}3. Implementation in webpage - This example sets the 'cmdSearch' button as the default button for the txtSearchLastName, cboSearchDistrict and cboSearchRole search controls. It also sets the 'cmdSave' button as the default button for the 'txtNewQ1', 'txtNewQ2', 'txtNewQ3', 'txtNewQ4' and 'txtComments' edit controls.
1private void Page_Load(object sender, System.EventArgs e)
2{
3 if (!Page.IsPostBack)
4 {
5 SetDefaultButtonForControl(this,
6 Enums.ControlTypes.TextBox, txtSearchLastName, cmdSearch);
7 SetDefaultButtonForControl(this,
8 Enums.ControlTypes.DropDownList, cboSearchDistrict, cmdSearch);
9 SetDefaultButtonForControl(this,
10 Enums.ControlTypes.DropDownList, cboSearchRole, cmdSearch);
11 SetDefaultButtonForControl(this,
12 Enums.ControlTypes.TextBox, txtNewQ1, cmdSave);
13 SetDefaultButtonForControl(this,
14 Enums.ControlTypes.TextBox, txtNewQ2, cmdSave);
15 SetDefaultButtonForControl(this,
16 Enums.ControlTypes.TextBox, txtNewQ3, cmdSave);
17 SetDefaultButtonForControl(this,
18 Enums.ControlTypes.TextBox, txtNewQ4, cmdSave);
19 SetDefaultButtonForControl(this,
20 Enums.ControlTypes.TextBox, txtComments, cmdSave);
21 }
22}
Comments
|
On
1/8/2010
Adamski
said:
Mate you are a star, was having a lot of problems trying to get all boxes access different button clicks so many options on one page ans this is the perfect fix. Tried somwthing similar but this is much more tidy.
On
1/8/2010
Brian Pautsch
said:
Thanks, Jeremy. One issue is that it doesn't work perfectly with .NET 2.0. In that case, I'm doing the following:
On
1/8/2010
Jeremy
said:
This was a really great post. It finally brought together and made sense of 3 common problems/issues that I've been seeing posts on. I think this one completely explains a solution to the issue you mention, while shining light on the others. Now I've got fixes for all of the problems.
On
1/8/2010
Brian Pautsch
said:
Yeah…I quickly tested the code as is in ASP.NET 2.0 and it didn’t work…bummer!
On
1/8/2010
Nikos Katravas
said:
Dropdown list case don't seem to work.
On
12/17/2008
Ravindra Lad
said:
This is really a great post and helps me a lot
On
10/13/2006
Dave
said:
Thanks Brian!!! I tried to do the same code in javascript, passing in the id of the control to 'click', but it only worked when I did it as you described. You saved me on this one! Thanks!
On
9/26/2006
Dennis
said:
ASP.NET 2.0 supports default buttons out of the box, so from what I understand, the above code is not needed anymore.
|
Leave a Comment