Take advantage of DefaultButton property in ASP.NET 2.0

If you are new to ASP.NET 2.0, then this would be good news to you! If you have more than one button on a form and if you wanted to keep a specific button as default one, then the DefaultButton property helps you to specify any button on a form as the default button when the form loads.
(For a new commer to the web development, the default button is the button which will post back to the server if you press the ENTER key)

 

For example, if you wanted to specify SubmitButton as a default button, then the syntax would be as shown below.

<form id="Form1"

    defaultbutton="SubmitButton"

    defaultfocus="TextBox1"

    runat="server">

 

Complete example:

<%@ page language="C#" %>

 

<html>

<head>

  <script runat="Server">    

  

    void Page_Load(object sender, System.EventArgs e)

    {

      Label1.Text = "The DefaultButton property is set to "

                    + Form1.DefaultButton.ToString() + "<br/>";

      Label2.Text = "The DefaultFocus property is set to "

                    + Form1.DefaultFocus.ToString();

     }

 

      void CancelButton_Click(object sender, EventArgs e)

      {

          Response.Write("Cancle is clicked");

      }

</script>

</head>

<body>

  <form id="Form1"

    defaultbutton="SubmitButton"

    defaultfocus="TextBox1"

    runat="server">

   

    <h3>DefaultButton and DefaultFocus Properties Example (2.0)</h3>        

 

    TextBox1:

    <asp:textbox id="TextBox1"

      autopostback="true"

      runat="server">

    </asp:textbox>

 

    <br />

 

    TextBox2:

    <asp:textbox id="TextBox2"

      autopostback="true"

      runat="server">

    </asp:textbox>

 

    <br /><br />

 

    <asp:button id="SubmitButton"

      text="Submit"

      runat="server">

    </asp:button>

 

    <asp:button id="CancelButton"

      text="Cancel"

      runat="server" OnClick="CancelButton_Click">

    </asp:button>

 

    <hr />

 

    <asp:label id="Label1"

      runat="Server">

    </asp:label>

 

    <asp:label id="Label2"

      runat="Server">

    </asp:label>

 

  </form>

</body>

</html>

 

Hope it helps you!

Published Monday, September 20, 2004 6:57 AM by SreedharK

Comments

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Monday, September 20, 2004 8:16 AM by Ramon Smits
Nice to have one defaultbutton.. is it mandatory to have autopostback enabled on all controls??

Now I want to have multiple forms and each form with it's own default button as we have always used prior to ASP.NET

It is a pain in the ass to only have one serverside form. Everyone it complaining that you shouldn't use framesets for obvious reasons. Not a problem if you can use multiple forms on one page (for example, one form for global site search, one for the main content, one for login in. This maintains quite well and even fits the ASP.NET control profile. It would map very well when you could create a form as a sort of control.

Each form has it's own default action and (clientside!) validation. If you want this behaviour in ASP.NET you must disable all client-site validations and depending button pressed you must enable the validators in a specific control and call the page Validate method and IsValid property to see if the submitted content is ok. This is stupid! When not implemented the page just submits itself and does nothing! Thus making a useless roundtrip!

I like ASP.NET very much but some thing like this really make my irritated because this was always really easy to use prior to ASP.NET. With helpers classes/baseclass methods and lot's of javascript it is possible to create the same experience for the end user but WHY should I have to do that with just ONE form when using multiple forms develops much easier?

Maybe it is just me who is irritated by this design/behaviour...

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Monday, September 20, 2004 9:14 AM by Alan Yeung
Good news, so I will no longer have to embed two javascripts (one for default button and one for default focus) on every form...

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Monday, September 20, 2004 9:57 AM by Sreedhar
Ramon:
For obvious reasons I was not in favour of having Multiple forms on the same page!

I guess your business requirement might demanded to use it! That would be one reason I can think why you gone for that design!
As an alternative see if Wizard control helps you now! (It won't be as multiple forms on the page, but it's more flexible for that kind of desing and it's an alternative!)

Alan:
Yep! You no longger need to two javascripts. how cool it is!!! :)

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Thursday, August 16, 2007 8:46 PM by Myron

interesting

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Monday, August 20, 2007 5:15 PM by Vassilis

Cool.

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Wednesday, August 22, 2007 6:29 AM by Thanos

Interesting...

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Tuesday, March 11, 2008 3:35 PM by Dave

You can have a DefaultButton set for each 'Panel' and put your sections of the form in Panels...

Not a solution, but a better way of working with what we've got.

(he says 3.5 years later)

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Thursday, July 17, 2008 9:49 AM by valli

With DefaultButton set for a panel, the javascript validation script given for the button, is not working properly. The validation script, whether it returns true or false, the server sider onclick triggers. I want it to be triggered only if javascript returns true ?!!!

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Friday, December 05, 2008 5:54 PM by Semil

<a href= spiritez.com ></a>

# re: Take advantage of DefaultButton property in ASP.NET 2.0

Tuesday, April 14, 2009 12:13 PM by viswavikass

Hi Valli,

I have similar requirements and, adding the defaultbutton attribute to the panel, worked for me.

Below is my code:

1. ASPX

   <form id="form1" runat="server">

   <div>

   <asp:Panel ID="panel1" runat="server" defaultbutton="btnsubmit1">

   <table runat="server" width="100%">

   <tr runat="server" style="width:100%">

       <td style="width:50%" align="right" colspan="1" runat="server">

           <asp:Label runat="server" id= "lblValue" Text="Value"/>

       </td>

       <td style="width:50%" align="left" colspan="1" runat="server">

           <asp:TextBox id="txtValue" runat="server"/>

       </td>

   </tr>

   <tr style="width:50%" align="center" runat="server">

       <td colspan="2" runat="server">

           <asp:Button ID="btnCancel" runat="server" Text="Cancel"

               onclick="btnCancel_Click" />

           <asp:button runat="server" id="btnsubmit1" OnClick="btnSubmit_Click" Text ="Click Here"></asp:button>            

       </td>

   </tr>

   </table>

    </asp:Panel>

   </div>

   </form>

2. Code-behind:

       protected void Page_Load(object sender, EventArgs e)

       {

           if(!IsPostBack)

           {

               btnsubmit1.Attributes.Add("OnClick", "return fnsubmit();");

               txtValue.Focus();

           }

        }

       protected void btnSubmit_Click(object sender, EventArgs e)

       {

           txtValue.Text = "submitted";

       }

       protected void btnCancel_Click(object sender, EventArgs e)

       {

           txtValue.Text = "cancelled";

       }

Thanks,

Viswanathan

Leave a Comment

(required) 
(required) 
(optional)
(required)