【问题标题】:ASP.NET - Default Button on form with dropdowns and button onlyASP.NET - 表单上的默认按钮,只有下拉菜单和按钮
【发布时间】:2012-08-27 17:22:48
【问题描述】:

我有一个 ASP.NET 页面,其中包含列表视图中的一些下拉列表。表单底部是一个提交按钮。

每当用户点击“Enter”键时,我都希望按下提交按钮。我已经在具有面板的 DefaultButton 属性的其他表单上完成了此操作,但那些在面板中有一个文本框和一个按钮。

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

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

那么当用户在键盘上按下“Enter”时,简单按下提交按钮的答案是什么?

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

【问题讨论】:

标签: asp.net


【解决方案1】:

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

希望它能解决您的问题。

【讨论】:

  • 表单标签通常在masterpage中,当他在其他页面上做同样的事情时会给他带来麻烦。
  • 可以在运行时在页面初始化时设置此属性,即 Page.Form.DefaultButton = "buttonID";
  • 我添加了这段代码 Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init Page.Form.DefaultButton = "btnQuery" End Sub 并收到此错误消息“The 'form1' 的 DefaultButton 必须是 IButtonControl 类型的控件的 ID。"
  • 我找到了错误的解决方案。我使用这个:Page.Form.DefaultButton = btnQuery.UniqueID。但是即使现在它执行了,它仍然没有“抓住”回车按钮。
【解决方案2】:

您需要在列表视图上方创建面板;

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

【讨论】:

    【解决方案3】:

    另一种方法是编写一个 javaScript sn -p 来检测“ Enter”按钮,然后提交表单:

        function yourKeyPressFunction(e) {
            if (e.keyCode == 13) {
                this.form.submit();
            }
        }
    
        <form method="post" onkeypress="yourKeyPressFunction(event)">
    

    【讨论】:

    • 它必须在 aspx 页面上。面板和/或表格可能有 onkeypress 事件。我不确定
    • 我正在使用母版页。表单标签在那里。我只需要在我的几个页面上执行此操作。
    • 您应该能够找到框架或面板的 onkeypress 事件。这仅取决于您页面上的元素类型
    猜你喜欢
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2018-07-26
    • 1970-01-01
    相关资源
    最近更新 更多