in my web application i have a master page and i want to implement defaultbutton for a login page when user press enter (my application has Master page) how can i place default button.
6 Answers
Page.Form.DefaultButton = crtlLoginUserLogin.FindControl("LoginButton").UniqueID
or just
Page.Form.DefaultButton = LoginButton.UniqueID
This will work.
-
I am using master page Mr. Five Tools how can it is possible – Surya sasidhar Feb 08 '10 at 04:23
-
2you can use the above code in the page load event of your login page - regardless of the master page that's related to it. The ctrlLoginUserLogin I mention above is an asp:login i have on a page in one of my apps. Works fine. – FiveTools Feb 08 '10 at 15:05
If you want to set a default button in a Master Page, and the button is in a Content Page or a User Control, you cannot set this directly in the Master Page markup.
<form id="form1" runat="server" defaultbutton="MyButton" >
Will generate the following error:
The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.
The workaround for this is to set the default button during the Page_Load of your Content/User Control:
protected void Page_Load(object sender, EventArgs e)
{
Button myButton = (Button)FindControl("MyButton");
Page.Form.DefaultButton = myButton.UniqueID;
}

- 2,257
- 24
- 28
The VB version of this is as follows:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'cast the master page form to set the default values for the Default button and Default focus'
Dim myForm As HtmlForm = TryCast(Me.Master.FindControl("myMasterForm"), HtmlForm)
myForm.DefaultButton = Me.btnAdd.UniqueID
myForm.DefaultFocus = Me.txtMyTextbox.UniqueID
End Sub

- 21
- 1
Nothing to do with Master Pages - have a look here for how Web Browsers interprept the forms.
Personally I would enclose the form in its own Panel control and set the defaultbutton property to that of the submit button.
NOTE: This will only work in ASP.NET 2.0 and above.

- 21,988
- 13
- 81
- 109

- 16,498
- 6
- 59
- 85
-
Actually in the link you posted, several people commented on issues with master pages and the default button – Brian Vander Plaats Aug 27 '10 at 20:59
IMHO, There is a BuiltIn Control designed for Login called as LoginView. It integrates into your Master page or any other page and could provide full use of the authentication system. Here is the code for it
<asp:LoginView ID="LoginView1" runat="server">
</asp:LoginView>
Asp.Net provides a complete framework for authentication and authorization of an application. I would recommend having a look at if you are about to implement one for your application and you have not reviewed it as option already.
EDIT: If you want a button to be place over master page, Drag and Drop the button like we do for a normal Web-form and Implement following event:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("MyApplication/SomePage.aspx");
}
Hope it Helps

- 21,468
- 17
- 69
- 94
-
Just Drag and Drop a button control Like we do for Web-Forms!! and implment the event Click for it. Check Edit – Asad Feb 06 '10 at 12:04
(Page.Master.FindControl("Form1") as HtmlForm).DefaultButton = this.cmdSubmit.UniqueID;
From http://www.dotnetthoughts.net/2010/06/21/asp-net-default-button-and-master-pages/

- 21,988
- 13
- 81
- 109

- 3,885
- 2
- 30
- 24