C# 当我按 Enter 时提交登录控制按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/201776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:47:48  来源:igfitidea点击:

Submit Login control button when I hit Enter

c#asp.net.netlogin-controldefaultbutton

提问by Ryan Lundy

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.

我有一个带有登录控件的 ASP.NET 网页。当我按下 Enter 键时,登录按钮不会触发;相反,页面提交,什么都不做。

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:

我在网上找到的标准解决方案是将登录控件包含在面板中,然后设置面板默认按钮。但显然,如果页面有母版页,那效果就不太好了。我已经尝试在代码中使用control.ID、control.ClientID 和control.UniqueID设置默认按钮,在每种情况下我都得到:

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

panelName 的 DefaultButton 必须是 IButtonControl 类型的控件的 ID。

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?

我确信有一种方法可以用 JavaScript 做到这一点,但如果可能的话,我真的很想用普通的旧 C# 代码来做到这一点。是否可以?

采纳答案by Vassili Altynikov

This should be helpful: http://weblogs.asp.net/jgalloway/archive/2007/10/03/asp-net-setting-the-defaultbutton-for-a-login-control.aspx

这应该会有所帮助:http: //weblogs.asp.net/jgalloway/archive/2007/10/03/asp-net-setting-the-defaultbutton-for-a-login-control.aspx

You can use the following to reference the button within the Login control template:

您可以使用以下内容来引用登录控件模板中的按钮:

DefaultButton="Login$LoginButton"

Basically, you can define a DefaultButton not just on the Form level, but also on individual Panel level, as long as the focus is within the panel, the default button for the panel will be used if you hit "Enter"

基本上,您不仅可以在表单级别定义 DefaultButton,还可以在单​​个面板级别定义 DefaultButton,只要焦点在面板内,如果您点击“Enter”,就会使用面板的默认按钮

回答by StingyHyman

you have to add something like this in page load...

你必须在页面加载中添加这样的东西......

txtPassword.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + lnkSubmit.UniqueID + "','')")

the password textbox has an onKeyPress attribute added that will force a doPostBack on the submit button if the "Enter" key is pressed. This simulates clicking the submit button.

密码文本框添加了一个 onKeyPress 属性,如果按下“Enter”键,它将在提交按钮上强制执行 doPostBack。这模拟单击提交按钮。

回答by roryf

Sounds like the login button is being spat out as <input type="button">rather than <input type="submit">. You could always template the LoginControl and add the submit button, getting rid of the hideous default markup at the same time!

听起来登录按钮被吐出<input type="button">而不是<input type="submit">。你总是可以模板化 LoginControl 并添加提交按钮,同时摆脱可怕的默认标记!

If you have to use Javascript to fix this something is seriously wrong! (but then it sounds like you know this)

如果你必须使用 Javascript 来解决这个问题,那就是严重错误了!(但听起来你知道这一点)

回答by EMP

Great answer by Blend Master! Essentially just use Panel.DefaultButton, but I want to clear up the confusion about what exactly you need to set it to. It's not just ".ID" or ".UniqueID" - the documentation is a bit lacking on this.

混合大师的好答案!基本上只使用 Panel.DefaultButton,但我想澄清一下您需要将它设置为什么的困惑。这不仅仅是“.ID”或“.UniqueID”——文档对此有点缺乏。

You must set it to the UniqueID of the button, relative tothe Panel's naming container UniqueID. For example, if your panel UniqueID is"Body$Content$pnlLogin" and your login button's UniqueID is "Body$Content$ucLogin$btnLogin" (because it's inside a control called "ucLogin") then you need to set Panel.DefaultButton to "ucLogin$btnLogin".

您必须将其设置为按钮的 UniqueID,相对于面板的命名容器 UniqueID。例如,如果您的面板 UniqueID 是“Body$Content$pnlLogin”,而您的登录按钮的 UniqueID 是“Body$Content$ucLogin$btnLogin”(因为它位于名为“ucLogin”的控件内),那么您需要将 Panel.DefaultButton 设置为“ucLogin$btnLogin”。

You can work this out in code as follows. (I couldn't find any class library method for this, but let me know if you find one.)

您可以按如下方式在代码中解决此问题。(我找不到任何用于此的类库方法,但如果您找到了请告诉我。)

void SetDefaultButton(Panel panel, IButtonControl button)
{
    string uniqueId = ((Control)button).UniqueID;
    string panelIdPrefix = panel.NamingContainer.UniqueID + Page.IdSeparator;

    if (uniqueId.StartsWith(panelIdPrefix))
    {
        uniqueId = uniqueId.Substring(panelIdPrefix.Length);
    }

    panel.DefaultButton = uniqueId;
}

回答by Kb.

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.

根据您的好答案,制作了一个自定义控件,
使页面能够根据处于焦点的面板具有多个默认按钮。
它覆盖了 Panel 的 OnLoad 方法和 DefaultButton 属性。

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

回答by qwebek

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>

回答by David Eison

Assuming Login1 is the ID of your login control.

假设 Login1 是您的登录控件的 ID。

For 'enter' anywhere on the page to submit, add to init in your codebehind:

对于要提交的页面上任何位置的“输入”,请在代码隐藏中添加到 init:

protected void Page_Init(object sender, EventArgs e)
{
    this.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;
}

For 'enter' to only submit when inside the login control, wrap the login control in an asp:Panel and set DefaultButton="Login1$LoginButton"on the panel

为了使“输入”仅在登录控件内部时提交,请将登录控件包装在 asp:Panel 中并DefaultButton="Login1$LoginButton"在面板上设置

Both approaches work fine with master pages.

这两种方法都适用于母版页。

回答by samuel

A solution is provided by embedding the login control inside the panel control and setting the defaultbuttonof the panel to the Parent$IDof the button inside Login control works. There's the code:

通过在面板控件中嵌入登录控件并将面板的 设置为登录控件中的按钮defaultbuttonParent$ID,提供了一种解决方案。有代码:

<asp:Panel id="panel1" runat="server" DefaultButton="Login1$LoginButton">
<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3">
<LayoutTemplate>
<table>
...
<tr>
<td><asp:Button ID="LoginButton" runat="server" /></td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
</asp:Panel>

回答by Jason Marsell

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.

在加载登录控件的任何页面的标记中,您需要在两个地方更新 html。

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标签中,需要设置默认按钮。请参阅下文,了解我是如何想出这个名字的。

<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)

(命名:美元符号之前的 ucLogin 部分需要是您的登录控件的 ID,正如在您的页面中进一步声明的那样。btnSubmit 部分需要是按钮的 ID,因为它在登录控件的 html 中被命名)

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

接下来,您需要将登录控件的声明包装在面板中,并设置它的 DefaultButton 属性,以及:

<!-- 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.

那应该为你做。