Search code examples
c#asp.nettextboxwebformsascx

Handling the submit action of two TextBoxes


I have an ASP.net page.
That has an Ajax Toolkit Tab Control.
That has tabs.
That have custom ascx controls I wrote.

I have a text box that perform a search action. It is declared like this:

            <asp:TextBox ID="txtPrereqSearch" runat="server"
                ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>

Nothing fancy. This format has been working for months. There's no submit button. It just posts back when I hit enter. The problem appeared when I added a second custom control using the same type of feature. Now browsers don't postback when I type something in either of these textboxes and press enter.

It seems that browsers have a default way of handling one textbox in one form, but that behavior changes when the number reaches two.

Is there an easy way around this? I guess I can create a hidden submit button but it seems like there is probably a better way to deal with this when the functionality is in two separate custom controls.

Your feedback is appreciated!


Solution

  • The answer was a little different than I expected, but philosophically like my original idea that @jamietre reinforced.

    I had to surround the controls with an <asp:Panel> tag with a DefaultButton attribute. A-like-a so:

    <asp:Panel ID="ButtonPanel" runat="server" DefaultButton="btnSubmit">
                            <asp:Label ID="Label1" runat="server" Text="Course:"></asp:Label>
                            <asp:TextBox ID="txtPrereqSearch" runat="server"
                                ontextchanged="txtPrereqSearch_TextChanged"></asp:TextBox>
                                <asp:TextBoxWatermarkExtender ID="txtPrereq_TextBoxWatermarkExtender" 
                        runat="server" Enabled="True" TargetControlID="txtPrereqSearch" 
                        WatermarkCssClass="Watermark" WatermarkText="e.g., MATH201"></asp:TextBoxWatermarkExtender>
                            <asp:Button ID="btnSubmit" CssClass="InvisibleSubmit" runat="server" Text="Submit" OnClick="txtPrereqSearch_TextChanged"/>
                        </asp:Panel>