Decrease Font Size
Increase Font Size
   BLOG

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

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();
    }

Comments (19) -

  • duffle

    11/10/2009 10:41:52 AM |

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

  • pay day loans

    12/28/2009 7:43:40 AM |

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

  • quick loans

    12/31/2009 6:05:26 PM |

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

  • auction online

    1/7/2010 1:48:44 PM |

    Hi,


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

  • SEO

    1/13/2010 6:35:05 PM |

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

  • bulk sms

    1/14/2010 3:49:29 AM |

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

  • SEO

    1/15/2010 9:42:31 AM |

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

  • Lildjan

    1/28/2010 11:12:56 AM |

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

  • business opportunity

    1/31/2010 9:49:32 PM |

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

  • japan

    1/31/2010 10:50:01 PM |

    Hi,



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

  • overseas removals

    2/6/2010 6:14:47 AM |

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

  • Women's Clothing

    2/18/2010 6:07:21 PM |

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

  • fha loan requirements

    2/28/2010 2:37:11 AM |

    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.

  • Philip

    3/1/2010 6:04:00 AM |

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

  • baby england kit

    3/6/2010 8:30:48 AM |

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

  • Search Engine Optimization Service

    3/11/2010 5:34:02 AM |

    phenomenon the beginning remains always the most notable moment.

  • Search Engine Optimization Services

    3/14/2010 4:44:02 AM |

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

  • auctions online

    3/14/2010 11:55:11 AM |

    Forwarded this to some friends, appreciate your advice

Add comment

Loading