user1247395
user1247395

Reputation: 419

ASP.NET - Default Button on form with dropdowns and button only

I have an ASP.NET page with some dropdown lists within a listview. At the bottom of the form is a submit button.

Whenever the user hits the "Enter" key I want the submit button pressed. I've done this on other forms with the DefaultButton attribute of a panel, but those had a textbox and a button in the panel.

The defaultbutton property is not triggering the submit button here, I think because the focus is not in the panel.

However if I make the panel include all of the elements this doesn't seem to work either. I think this is because of the html table layout elements.

So what is the answer to simple press the submit button when the user presses "Enter" on the keyboard?

<table cellpadding="3" cellspacing="1" border="0" width="100%">
    <tr>
        <td align="center" width="100%" class="tableCaptionLabel" colspan="2">
        Asset Search by Photo
        </td>
    </tr>
    <tr valign="top">
        <td colspan="2">



        <asp:ListView ID="lvQuery" runat="server" >
            <LayoutTemplate>
            <asp:Placeholder
            id="groupPlaceholder"
            runat="server" />
            </LayoutTemplate>
            <GroupTemplate>
            <div>
            <asp:Placeholder
            id="itemPlaceholder"
            runat="server" />
            </div>
            </GroupTemplate>
            <ItemTemplate>
            <asp:DropDownList ID="tagname" runat="server" DataSourceID="sqldsTagList" SelectedValue='<%# Bind("Tagname") %>'
                    DataTextField="TagDesc" DataValueField="TagDesc"  AppendDataBoundItems ="true" AutoPostBack="true" OnSelectedIndexChanged="lstTagname_SelectedIndexChanged">
                    <asp:ListItem Text="All" Value="All" />
            </asp:DropDownList>

               <asp:DropDownList ID="Operation" runat="server" SelectedValue='<%# Bind("Operation") %>' AutoPostBack="true" OnSelectedIndexChanged="lstOperation_SelectedIndexChanged">
                    <asp:ListItem Text="Equals" Value="Equals" Selected="True"/>
                    <asp:ListItem Text="Like" Value="Like"  />
               </asp:DropDownList>

            <asp:TextBox ID="TagValue" runat="server" Text='<%# Bind("TagValue") %>' Width="75%"></asp:TextBox>

            <asp:DropDownList ID="ddlAlphabet" runat="server" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="lstAlphabet_SelectedIndexChanged">
                <asp:ListItem Text="A" Value="A" Selected="True"/>
                <asp:ListItem Text="B" Value="B"  />
                <asp:ListItem Text="C" Value="C"  />
                <asp:ListItem Text="D" Value="D"  />
                <asp:ListItem Text="E" Value="E"  />
                <asp:ListItem Text="F" Value="F"  />
                <asp:ListItem Text="G" Value="G"  />
                <asp:ListItem Text="H" Value="H"  />
                <asp:ListItem Text="I" Value="I"  />
                <asp:ListItem Text="J" Value="J"  />
                <asp:ListItem Text="K" Value="K"  />
                <asp:ListItem Text="L" Value="L"  />
                <asp:ListItem Text="M" Value="M"  />
                <asp:ListItem Text="N" Value="N"  />
                <asp:ListItem Text="O" Value="O"  />
                <asp:ListItem Text="P" Value="P"  />
                <asp:ListItem Text="Q" Value="Q"  />
                <asp:ListItem Text="R" Value="R"  />
                <asp:ListItem Text="S" Value="S"  />
                <asp:ListItem Text="T" Value="T"  />
                <asp:ListItem Text="U" Value="U"  />
                <asp:ListItem Text="V" Value="V"  />
                <asp:ListItem Text="W" Value="W"  />
                <asp:ListItem Text="X" Value="X"  />
                <asp:ListItem Text="Y" Value="Y"  />
                <asp:ListItem Text="Z" Value="Z"  />
                <asp:ListItem Text="0" Value="0"  />
                <asp:ListItem Text="1" Value="1"  />
                <asp:ListItem Text="2" Value="2"  />
                <asp:ListItem Text="3" Value="3"  />
                <asp:ListItem Text="4" Value="4"  />
                <asp:ListItem Text="5" Value="5"  />
                <asp:ListItem Text="6" Value="6"  />
                <asp:ListItem Text="7" Value="7"  />
                <asp:ListItem Text="8" Value="8"  />
                <asp:ListItem Text="9" Value="9"  />
            </asp:DropDownList>
            </ItemTemplate>
            <EmptyItemTemplate>           

            </EmptyItemTemplate>
        </asp:ListView>


        </td>
    </tr>
    <tr>
        <td>
        <asp:Panel runat="server" ID="pnlEnter" DefaultButton="btnQuery" >
        <asp:Button ID="btnQuery" runat="server" Text="Submit" />
        </asp:Panel>
        </td>
        <td align="right">
        <asp:Button ID="btnAddRow" runat="server" Text="Add Row" />
        </td>
    </tr>
</table>

Upvotes: 1

Views: 1309

Answers (3)

MikeTWebb
MikeTWebb

Reputation: 9279

Another approach would be to write a javaScript snippet the detects the " Enter" button and then submits the form:

    function yourKeyPressFunction(e) {
        if (e.keyCode == 13) {
            this.form.submit();
        }
    }

    <form method="post" onkeypress="yourKeyPressFunction(event)">

Upvotes: 0

Idrees Khan
Idrees Khan

Reputation: 7752

You need to create panel above the listview;

<asp:Panel runat="server" ID="pnlEnter" DefaultButton="btnQuery">   // panel should be at top of listview
       <asp:ListView ID="lvQuery" runat="server" >
          .......
          ........
       </aspListView>
       <asp:Button ID="btnQuery" runat="server" Text="Submit" />         
</asp:Panel> 

Upvotes: 0

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14302

Set the DefaultButton property in form tag instead of panel and also set the property of UseSubmitBehavior="true" in your asp:button.

Hopefully it will fix your issue.

Upvotes: 2

Related Questions