Thursday, December 11, 2008

Login control default action problem

There is a bug in the Login control when you are using an image for the button, the default action will not be triggered. To overcome this, first wrap your Login control in a simple Panel:

<asp:Panel ID="LoginSubmitPanel" DefaultButton="" runat="server">
    <%--Login Control Here--%>
</asp:Panel>


Then add a load event to that panel to look for the unique id of the image button and assign the default action for this panel:

protected void LoginSubmitPanel_Load(object sender, EventArgs e)
{
    //Find all the controls we will need
    Login SideLogin = (sender as Login);
    Control LoginButton = (SideLogin.FindControl("LoginImageButton") as Control);
    Panel LoginSubmitPanel = (SideLoginView.FindControl("LoginSubmitPanel") as Panel);
    //We need the UniqueName under the proper context
    string btn = LoginButton.UniqueID.Remove(0, SideLoginView.UniqueID.Length + 1);
    //Assign the correct button as the default action        
    LoginSubmitPanel.DefaultButton = btn;
}


Now when you hit the enter key in the login control, the login action will be fired. Outside of the login control will follow regular default form submission.

2 comments:

  1. I was about to comment that I have never had that problem when using images for buttons, but then I realized that I'm not even using the login control! That might be why. :)

    ReplyDelete
  2. You are lucky. That control drives me crazy.

    ReplyDelete