views:

315

answers:

5

Hello I have a asp.net webpage, it has two search fields with separate submit buttons.

I'd like to allow the user to enter some data into one of the fields and then press the enter key and it submit using the appropriate button. Right now, the enter key submits for the first submit button, but I want this to change if there is data in the second textbox.

How can I do this?

Currently my code is:

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

Can you split it into two separate <form>s? You'd only ever get one of the text fields submitted (without JavaScript trickery) but maybe that doesn't matter in your application?

RichieHindle
+2  A: 

Have a look at the DefaultButton property. You can wrap each of those label/button pairs in a panel and set the property to btnEmployeeSearch and btnCustomerSearch, respectively.

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>
Jon Freeland
+2  A: 

I think it might be better to have both buttons trigger the same server-side function then you can check which button was clicked programmatically and then call other methods from there.

Andrew Hare
Or rather have then trigger the same function and check whether the second textbox was empty or not.
T Pops
+1  A: 

You can wrap each section in a panel and use it's defaultbutton property to specify the submit button for each textbox:

<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

Michael La Voie
A: 

Here is an extension of asp:Panel that enables a Page to have several default buttons
based on current focus.
It overrides Panel's OnLoad method and DefaultButton property.
You could wrap your fields and buttons in to separate custom panels and initiate their DefaultButton in Page_Load() or On_Load()

Here is the Custom Control:

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

    }
Kb