在运行时确定要使用的提交按钮

时间:2009-05-14 19:32:06

标签: .net asp.net webforms

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

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

我该怎么做?

目前我的代码是:

    <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" />

5 个答案:

答案 0 :(得分:2)

查看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>

答案 1 :(得分:2)

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

答案 2 :(得分:1)

您可以在面板中包装每个部分并使用它的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

答案 3 :(得分:0)

你能把它分成两个独立的<form>吗?你只会得到一个提交的文本字段(没有JavaScript技巧)但是在你的应用程序中这可能没关系吗?

答案 4 :(得分:0)

这是asp:Panel的扩展,它使Page能够根据当前焦点设置多个默认按钮 它会覆盖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;
            }
        }

    }