Surya sasidhar
Surya sasidhar

Reputation: 30303

Default button in asp.net (Master Page)?

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.

Upvotes: 6

Views: 20312

Answers (6)

Manuel Alves
Manuel Alves

Reputation: 4013

(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/

Upvotes: 0

David Neale
David Neale

Reputation: 17018

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.

Upvotes: 1

FiveTools
FiveTools

Reputation: 6030

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

or just

Page.Form.DefaultButton = LoginButton.UniqueID

This will work.

Upvotes: 17

user1346788
user1346788

Reputation: 21

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

Upvotes: 2

Brian Vander Plaats
Brian Vander Plaats

Reputation: 2287

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

Upvotes: 3

Asad
Asad

Reputation: 21928

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

Upvotes: 1

Related Questions