ASP.NET-窗体上的默认按钮,仅包含下拉列表和按钮

ASP.NET-窗体上的默认按钮,仅包含下拉列表和按钮,asp.net,Asp.net,我有一个ASP.NET页面,在listview中有一些下拉列表。表单底部有一个提交按钮 每当用户点击“回车”键时,我希望按下提交按钮。我在其他具有面板DefaultButton属性的表单上也这样做过,但是这些表单在面板中有一个文本框和一个按钮 defaultbutton属性没有触发这里的submit按钮,我想是因为焦点不在面板中 但是,如果我让面板包含所有元素,这似乎也不起作用。我认为这是因为html表格布局元素 那么,当用户按下键盘上的“回车”键时,简单地按下提交按钮的答案是什么呢 <t

我有一个ASP.NET页面,在listview中有一些下拉列表。表单底部有一个提交按钮

每当用户点击“回车”键时,我希望按下提交按钮。我在其他具有面板DefaultButton属性的表单上也这样做过,但是这些表单在面板中有一个文本框和一个按钮

defaultbutton属性没有触发这里的submit按钮,我想是因为焦点不在面板中

但是,如果我让面板包含所有元素,这似乎也不起作用。我认为这是因为html表格布局元素

那么,当用户按下键盘上的“回车”键时,简单地按下提交按钮的答案是什么呢

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

通过照片搜索资产

在表单标签而不是面板中设置
DefaultButton
属性,并在asp:button中设置
UseSubmitBehavior=“true”
属性


希望它能解决您的问题。

您需要在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> 
//面板应位于listview的顶部
.......
........

另一种方法是编写一个javaScript代码段来检测“ “输入”按钮,然后提交表格:

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

    <form method="post" onkeypress="yourKeyPressFunction(event)">
功能按键功能(e){
如果(e.keyCode==13){
这个.form.submit();
}
}

我建议使用jquery或类似的方法来处理按键:表单标签通常在母版页中,当他在其他页面上执行相同的操作时,会给他带来问题。好的,在运行时在page init上设置此属性,即page.form.DefaultButton=“buttonID”;我添加了这个受代码保护的子页面_Init(ByVal sender作为对象,ByVal e作为System.EventArgs)处理我。Init Page.Form.DefaultButton=“btnQuery”End Sub并得到了这个错误消息“form1”的DefaultButton必须是IButonControl类型的控件的ID。”我找到了错误的解决方案。我使用的是:Page.Form.DefaultButton=btnQuery.UniqueID。但是即使现在它执行了,它仍然没有“捕获”回车按钮。它应该在aspx页面上。面板和/或表可能有onkeypress事件。我不确定我用的是母版页。表单标签在那里。我只需要在我的几个页面上执行此操作。您应该能够找到框架或面板的onkeypress事件。这取决于页面上元素的类型