How to set default button (inside loginview) located on the master page

Last post 05-10-2008 6:32 AM by Dave Sussman. 4 replies.

Sort Posts:

  • How to set default button (inside loginview) located on the master page

    05-09-2008, 12:36 AM

    Hi guys,
    Here's my scenario. I have two buttons on the master page. A search button and a Login button. The search button is on the form, the login button is inside the loginview control which is also inside the form.
    I tried to set the login button as the default button by using the following code:

    Dim loginView As LoginView = DirectCast(Master.FindControl("LoginView1"), LoginView)
    Dim btnLogin As Button = DirectCast(loginView.FindControl("LoginButton"), Button)
    Me.Page.Form.DefaultButton = btnLogin.UniqueID

    I run the above code under the page load event for the defaultpage for trial. Instead of getting it to work, I got this error:

     

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 5:      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Line 6:          Dim loginView As LoginView = DirectCast(Master.FindControl("LoginView1"), LoginView)
    Line 7:          Dim btnLogin As Button = DirectCast(loginView.FindControl("LoginButton"), Button)
    Line 8:  
    Line 9:          Me.Page.Form.DefaultButton = btnLogin.UniqueID

    Any clue what I did wrong?  

    All helps are appreciated. Thanks

  • Re: How to set default button (inside loginview) located on the master page

    05-09-2008, 10:28 AM

    Are you using the LoginView purely to hide the menu once users have logged in? If so, you may just prefer to set the VisibleWhenLoggedIn property of the Login control to true. This means you can move the Login control outside of the LoginView and won't have any trouble setting the default button on the form declaratively.

  • Re: How to set default button (inside loginview) located on the master page

    05-09-2008, 5:23 PM
    • Loading...
    • jeff_way
    • Joined on 02-23-2007, 10:23 PM
    • Nashville
    • Posts 253
    Are you putting your code in the Page Load event of the master page or the content page? If the loginview is on the master page and your code behind is in the content page...it won't work. This is because the content page loads before the master page does. As a result, you'll be setting the default button to a control that hasn't been created yet. You can add a default button to the form tag. defaultButton="loginview1$myButton"
    Jeffrey Way | My Blog

    I update my blog on a daily basis with views on web development. I always appreciate new readers.
  • Re: How to set default button (inside loginview) located on the master page

    05-10-2008, 12:25 AM

    Yeah, I set the code for the default button on the code behind for the content page.

    Now, what is the syntax for setting default button on the form tag if the my login button has ID="LoginButton" and it's inside the LoginView control with ID="LoginView1"

    I tried <form id="form1" runat="server" defaultbutton="LoginButton"> on the master page, but when I run it, I got the following error:

     

    Server Error in '/TexasWC' Application.

    The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [InvalidOperationException: The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.]
       System.Web.UI.HtmlControls.HtmlForm.RenderAttributes(HtmlTextWriter writer) +1356
       System.Web.UI.HtmlControls.HtmlControl.RenderBeginTag(HtmlTextWriter writer) +39
       System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +56
       System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
       System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +37
       System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
       System.Web.UI.Control.Render(HtmlTextWriter writer) +7
       System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
       System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
       System.Web.UI.Control.Render(HtmlTextWriter writer) +7
       System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
       System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
       System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
       System.Web.UI.Page.Render(HtmlTextWriter writer) +26
       System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
       System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2558
    

     

     

  • Re: How to set default button (inside loginview) located on the master page

    05-10-2008, 6:32 AM

    You can't set the DefaultButton because the button is within a template and therefore doesn't exist until the template is generated. Also, when the user is logged in, the LoginTemplate is not visible, so the LoginButton wouldn't exist and you'd get the same exception.

    You're best bet is to not use a LoginView for this particular scenario; just have the LoginButton on the page, the same as the search button, and control it within code. Something like this (not tested):

    If Not Page.IsPostBack Then

      If Not HttpContext.Current.User.Identity.IsAuthenticated Then

        LoginButton.Visible = True

       Page.Form.DefaultButton = "LoginButton"

      Else

        LoginButton.Visible = False 

      End If

    End If

Page 1 of 1 (5 items)