【问题标题】:Determine at runtime which submit button to use在运行时确定使用哪个提交按钮
【发布时间】:2009-05-14 19:32:06
【问题描述】:

net 网页,它有两个带有单独提交按钮的搜索字段。

我想允许用户在其中一个字段中输入一些数据,然后按回车键并使用相应的按钮提交。现在,第一个提交按钮的回车键提交,但如果第二个文本框中有数据,我希望它改变。

我该怎么做?

目前我的代码是:

    <label for="ctl00_cpMain_txtEmployeeSearch">Employee Name:</label><asp:TextBox id="txtEmployeeSearch" runat="server" CssClass="TextBox" />
    <asp:Button ID="btnEmployeeSeach" runat="server" Text="Search" onclick="btnEmployeeSeach_Click" CssClass="button" />


    <label for="ctl00_cpMain_txtCustomerSearch">Customer Name:</label><asp:TextBox id="txtCustomerSearch" runat="server" CssClass="TextBox" />
    <asp:Button ID="btnCustomerSeach" runat="server" Text="Search" onclick="btnCustomerSeach_Click" CssClass="button" />

【问题讨论】:

  • 你知道jquery吗,因为这在jquery中很简单。如果是这样我会给你解决方案..问一下

标签: .net asp.net webforms


【解决方案1】:

查看DefaultButton 属性。您可以将每个标签/按钮对包装在一个面板中,并将属性分别设置为btnEmployeeSearchbtnCustomerSearch

http://forums.asp.net/t/985791.aspx

<asp:Panel DefaultButton="btnEmployeeSearch" runat="server" id="Panel">
    <label for="ctl00_cpMain_txtEmployeeSearch">Employee Name:</label><asp:TextBox id="txtEmployeeSearch" runat="server" CssClass="TextBox" />
    <asp:Button ID="btnEmployeeSeach" runat="server" Text="Search" onclick="btnEmployeeSeach_Click" CssClass="button" />
</asp:Panel>

【讨论】:

    【解决方案2】:

    我认为让两个按钮触发相同的服务器端功能可能会更好,然后您可以通过编程检查哪个按钮被单击,然后从那里调用其他方法。

    【讨论】:

    • 或者更确切地说,然后触发相同的功能并检查第二个文本框是否为空。
    【解决方案3】:

    您可以将每个部分包装在一个面板中,并使用它的 defaultbutton 属性来指定每个文本框的提交按钮:

    <asp:Panel runat="server" DefaultButton="btnEmployeeSeach">
        <label for="ctl00_cpMain_txtEmployeeSearch">Employee Name:</label><asp:TextBox id="txtEmployeeSearch" runat="server" CssClass="TextBox" />
        <asp:Button ID="btnEmployeeSeach" runat="server" Text="Search" onclick="btnEmployeeSeach_Click" CssClass="button" />
    </asp:Panel>    
    <asp:Panel runat="server" DefaultButton="btnCustomerSeach">
        <label for="ctl00_cpMain_txtCustomerSearch">Customer Name:</label><asp:TextBox id="txtCustomerSearch" runat="server" CssClass="TextBox" />
        <asp:Button ID="btnCustomerSeach" runat="server" Text="Search" onclick="btnCustomerSeach_Click" CssClass="button" />
    </asp:Panel>
    

    How to Set a Default Button

    【讨论】:

      【解决方案4】:

      你能把它分成两个独立的&lt;form&gt;s吗?您只会收到提交的文本字段之一(没有 JavaScript 技巧),但也许这在您的应用程序中并不重要?

      【讨论】:

        【解决方案5】:

        这里是 asp:Panel 的扩展,它使页面能够根据当前焦点具有多个默认按钮

        它覆盖了 Panel 的 OnLoad 方法和 DefaultButton 属性。
        您可以将字段和按钮包装到单独的自定义面板中,并在 Page_Load() 或 On_Load() 中启动它们的 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;
                    }
                }
        
            }
        

        【讨论】:

          猜你喜欢
          • 2010-10-02
          • 1970-01-01
          • 2014-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多