2012-08-27 67 views
1

我有一个ASP.NET页面,在列表视图中有一些下拉列表。表单底部是一个提交按钮。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> 
+0

我会建议使用jQuery或类似的处理按键的东西: http://api.jquery.com/keypress/ – CodingGorilla

回答

2

坐落在表单标签,而不是面板DefaultButton属性,并且还的UseSubmitBehavior="true"在你的ASP属性:按钮。

希望它能解决您的问题。

+0

表单标签通常在主页面中,当他在其他页面上做同样的事情时,它会给他带来问题。 –

+0

可以在页面init上运行时设置此属性,即Page.Form.DefaultButton =“buttonID”; –

+0

现在,这是他可能感兴趣的 –

0

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

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

这没有奏效。 – user1247395

0

另一种方法是写一段JavaScript代码的检测“ 输入”按钮,然后提交表单:

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

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

我没有表格标签。我将如何在代码隐藏中使用它? – user1247395

+0

它必须在aspx页面上。面板和/或桌子可能有onkeypress事件。我不确定那 – MikeTWebb

+0

我正在使用母版页。表单标签在那里。我只需要在我的几个页面上执行此操作。 – user1247395