Decrease Font Size
Increase Font Size
   BLOG

ASP.NET - How to set DefaultButton in a page that are using master page

by bryian 24. August 2009 20:43

Watch this Script in action

ASP.NET DefaultButton with MasterPages.

How To set Default Button for ENTER key pressed event

How to set DefaultButton in a page that are using master page.

Sometimes we might have two or more ASP.NET button control on the ASP.NET page and prefer certain button to cause postback when the ENTER key is pressed. The DefaultButton attributes on the HtmlForm control allow us to set the button control that will causes postback when the ENTER key is pressed. Here is a small demonstration on how to set the default button in a page with using master page.

.aspx page

<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server">
 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />  <br />
    <asp:Button ID="Button1" runat="server" OnClientClick="javascript:alert('btn 1');" Text="Button 1" OnClick="Button1_Click" />
    <br />
    <asp:Button ID="Button2" runat="server" OnClientClick="javascript:alert('btn 2');" Text="Button 2" OnClick="Button2_Click" />
   <br />
    <asp:Button ID="Button3" runat="server" OnClientClick="javascript:alert('btn 3');" Text="Button 3" OnClick="Button3_Click" />
    <br />
   <asp:Button ID="Button4" runat="server" OnClientClick="javascript:alert('btn 4');" Text="Button 4" OnClick="Button4_Click" />
  <br />  <br />  <br />  <br />
</asp:Content>

.cs page

protected void Page_Load(object sender, EventArgs e)
    {
        Page.Form.DefaultButton = Button4.UniqueID;
        Page.SetFocus(TextBox1);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "You typed in: " + TextBox1.Text + " You clicked on button1 on " + DateTime.Now.ToLongTimeString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Text = "You typed in: " + TextBox1.Text + " You clicked on button2 on " + DateTime.Now.ToLongTimeString();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Label1.Text = "You typed in: " + TextBox1.Text + " You clicked on button3 on " + DateTime.Now.ToLongTimeString();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Label1.Text = "You typed in: " + TextBox1.Text + " You clicked on button4 on " + DateTime.Now.ToLongTimeString();
    }

Tags: ,

ASP.NET

Comments

11/23/2009 11:41:52 PM #

duffle

Looking forward for more such stuff. Loved your style of providing the information and the nature of the content.

duffle United Kingdom

1/10/2010 8:43:40 PM #

pay day loans

Success is the maximum utilisation of the ability that you have.

pay day loans United States

1/14/2010 7:05:26 AM #

quick loans

In every phenomenon the beginning remains always the most notable moment.

quick loans United States

1/21/2010 2:48:44 AM #

auction online

Hi,


Really appreciate this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!

auction online United States

1/27/2010 7:35:05 AM #

SEO

Why didn’t I find this post earlier? Keep up the good work!

SEO United States

1/27/2010 4:49:29 PM #

bulk sms

You really know your stuff... Keep up the good work!

bulk sms United States

1/28/2010 10:42:31 PM #

SEO

I’m impressed, you know what you’re talking about..

SEO United States

1/31/2010 11:56:36 PM #

baby clothing

Awesome tips. I’ll be passing this post on for sure
http://www.babybees.com.au

baby clothing United States

2/11/2010 12:12:56 AM #

Lildjan

Nice post! I like this article. Very informative. I will share it to my friends.

Lildjan Republic of the Philippines

2/14/2010 10:49:32 AM #

business opportunity

Awesome tips. I’ll be passing this post on for sure

business opportunity United States

2/14/2010 11:50:01 AM #

japan

Hi,



Don’t stop writing, you’ve given me lots of good info!

japan United States

2/19/2010 7:14:47 PM #

overseas removals

Useful info. Hope to see more good posts in the future.

overseas removals United Kingdom

3/4/2010 7:07:21 AM #

Women's Clothing

I tried the link or the buttons. It is cool. It would pop up a box telling you what button you pressed.

Women's Clothing United States

3/13/2010 3:37:11 PM #

fha loan requirements

This is a pretty good place for information, it’s the la quinta of the internet! Haha.  We'll im sure I will be back soon.

fha loan requirements United States

3/14/2010 7:04:00 PM #

Philip

Not at all ordinary, this is actually very interesting probably because its about defaultbutton.

Philip Nepal

3/19/2010 9:30:48 PM #

baby england kit

cheers mate, I woke up this morning and all i could think was this. I appreciate you sharing your two cents.

baby england kit United Kingdom

3/24/2010 6:34:02 PM #

Search Engine Optimization Service

phenomenon the beginning remains always the most notable moment.

Search Engine Optimization Service United States

3/27/2010 5:44:02 PM #

Search Engine Optimization Services

I find this post earlier? Keep up the good work!

Search Engine Optimization Services United States

3/28/2010 12:55:11 AM #

auctions online

Forwarded this to some friends, appreciate your advice

auctions online United States

Add comment


(Will show your Gravatar icon)

  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading