6

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.

Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

6 Answers6

17
Page.Form.DefaultButton = crtlLoginUserLogin.FindControl("LoginButton").UniqueID

or just

Page.Form.DefaultButton = LoginButton.UniqueID

This will work.

Jorge Lu
  • 3
  • 2
FiveTools
  • 5,970
  • 15
  • 62
  • 84
  • I am using master page Mr. Five Tools how can it is possible – Surya sasidhar Feb 08 '10 at 04:23
  • 2
    you 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
3

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;
        }
Brian Vander Plaats
  • 2,257
  • 24
  • 28
2

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
1

Nothing to do with Master Pages - have a look here for how Web Browsers interprept the forms.

Link

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
David Neale
  • 16,498
  • 6
  • 59
  • 85
1

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

Asad
  • 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
0
(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/

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Manuel Alves
  • 3,885
  • 2
  • 30
  • 24