Submit Login control button when I hit Enter

2020-01-30 07:46发布

I have an ASP.NET web page with a Login control on it. When I hit Enter, the Login button doesn't fire; instead the page submits, doing nothing.

The standard solution to this that I've found online is to enclose the Login control in a Panel, then set the Panel default button. But apparently that doesn't work so well if the page has a master page. I've tried setting the default button in code with control.ID, control.ClientID, and control.UniqueID, and in each case I get:

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

I'm sure there's a way to do this with JavaScript, but I'd really like to do it with plain old C# code if possible. Is it possible?

9条回答
聊天终结者
2楼-- · 2020-01-30 08:21

Yo, i found this solution on the net, it worked for me.

 <asp:Panel ID="panelLogin" runat="server" DefaultButton="Login1$LoginButton">
 <asp:Login ID="Login1" runat="server" >
 <LayoutTemplate>
 ...
 <asp:Button  ID="LoginButton" .../>
 </LayoutTemplate>
 </asp:Login>
 </asp:Panel>
查看更多
倾城 Initia
3楼-- · 2020-01-30 08:21

To add a bit more detail and instructions to the posts above, here's a walkthrough:

In the markup of any pages that load your login control, you need to update the html in two places.

First, in the page's form tag, you need to set the default button. See below for how I came up with the name.

<form id="form1" runat="server" defaultbutton="ucLogin$btnSubmit">

(Naming: The ucLogin part before the dollar sign needs to be the ID of your login control, as declared further down in your page. The btnSubmit part needs to be the ID of the button as it’s named in the login control’s html)

Next, you need to wrap the declaration of your login control in a panel, and set it’s DefaultButton property, as well:

<!-- Login Control - use a panel so we can set the default button -->
<asp:Panel runat="server" ID="loginControlPanel" DefaultButton="ucLogin$btnSubmit">                         
     <uc:Login runat="server" ID="ucLogin"/>                                                    
</asp:Panel>

That should do it for you.

查看更多
Evening l夕情丶
4楼-- · 2020-01-30 08:23

Based on your good answers, made a custom control that
enables a Page to have several default buttons based on which panel which is in focus.
It overrides Panel's OnLoad method and DefaultButton property.

public class DefaultButtonPanel:Panel
{
    protected override void OnLoad(EventArgs e)
    {
        if(!string.IsNullOrEmpty(DefaultButton))
        {
            LinkButton btn = FindControl(DefaultButton) as LinkButton;
            if(btn != null)
            {
                Button defaultButton = new Button {ID = DefaultButton.Replace(Page.IdSeparator.ToString(), "_") + "_Default", Text = " "};
                defaultButton.Style.Add("display", "none");
                PostBackOptions p = new PostBackOptions(btn, "", null, false, true, true, true, true, btn.ValidationGroup);
                defaultButton.OnClientClick = Page.ClientScript.GetPostBackEventReference(p) + "; return false;";
                Controls.Add(defaultButton);
                DefaultButton = defaultButton.ID;
            }
        }
        base.OnLoad(e);
    }

    /// <summary>
    /// Set the default button in a Panel.
    /// The UniqueID of the button, must be relative to the Panel's naming container UniqueID. 
    /// 
    /// For example:
    /// Panel UniqueID is "Body$Content$pnlLogin" 
    /// Button's UniqueID is "Body$Content$ucLogin$btnLogin" 
    /// (because it's inside a control called "ucLogin") 
    /// Set Panel.DefaultButton to "ucLogin$btnLogin".
    /// </summary>
    /// <param name="panel"></param>
    /// <param name="button"></param>
    public override string DefaultButton
    {
        get
        {
            return base.DefaultButton;
        }

        set
        {
            string uniqueId = value;
            string panelIdPrefix = this.NamingContainer.UniqueID + Page.IdSeparator;
            if (uniqueId.StartsWith(panelIdPrefix))
            {
                uniqueId = uniqueId.Substring(panelIdPrefix.Length);
            }
            base.DefaultButton = uniqueId;
        }
    }      
}
查看更多
登录 后发表回答