Greetings!
I have a simple navigation menu consisting of an asp:DropDownList and an asp:Button. Users select an item from the dropdown and click the button to go to the new URL. I'd like to be able to able to support when users hit the ENTER key when a dropdown list item is selected so that it replicates the behavior as if they've clicked the button.
我有一个简单的导航菜单,包含一个asp:DropDownList和一个asp:Button。用户从下拉列表中选择一个项目,然后单击按钮转到新URL。我希望能够支持用户在选择下拉列表项时按下ENTER键,以便复制行为,就好像他们点击了按钮一样。
Here's what I have thus far:
这是我到目前为止的情况:
<asp:DropDownList ID="ddlMenu"
runat="server"
onkeypress="if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {__doPostBack('GoButton',''); return false;}" />
<asp:Button ID="btnGoButton" runat="server" onclick="GoButton_Click"/>
The button's click code is:
按钮的点击代码是:
protected void GoButton_Click(object sender, EventArgs e)
{
string l_url = ddlMenu.SelectedItem.Value;
Response.Redirect(l_url);
}
However, everytime I hit the ENTER key, the page posts back but the button's client event handler doesn't fire. Am I missing something?
但是,每次按下ENTER键时,页面都会回发,但按钮的客户端事件处理程序不会触发。我错过了什么吗?
2 个解决方案
#1
have your tried wrapping your controls in a panel with the default button? I know it works for textboxes
您是否尝试使用默认按钮将控件包装在面板中?我知道它适用于文本框
<asp:Panel runat="server" id="searchPanel" DefaultButton="btnGoButton">
<asp:DropDownList ID="ddlMenu" runat="server" />
<asp:Button ID="btnGoButton" runat="server" onclick="GoButton_Click"/>
</asp:Panel>
#2
Well, you are using the wrong button id.
好吧,你使用的是错误的按钮ID。
The ID in the __doPostBack method must be the UniqueID of the trigger control. You say the trigger is 'GoButton', but it has the ID 'btnGoButton'.
__doPostBack方法中的ID必须是触发器控件的UniqueID。你说触发器是'GoButton',但它有ID'btnGoButton'。
There is no way for the server to know that your 'GoButton' actually is the btnGoButton.
服务器无法知道你的'GoButton'实际上是btnGoButton。
Also remember that name mangling (a method to ensure unique names for controls) may mess up the UniqueID even more.
还要记住,名称修改(一种确保控件的唯一名称的方法)可能会使UniqueID更加混乱。
Try to write like this instead: __doPostBack('<%# btnGoButton.UniqueID %>', '');
尝试这样写:__ doPostBack('<%#btnGoButton.UniqueID%>','');
or append it in the codebehind..
或将其附加在代码隐藏中..
ddlMenu.Attributes.Add("onkeypress", string.Format("__doPostBack('{0}', '');", btnGoButton.UniqueID);
ddlMenu.Attributes.Add(“onkeypress”,string.Format(“__ doPostBack('{0}','');”,btnGoButton.UniqueID);
#1
have your tried wrapping your controls in a panel with the default button? I know it works for textboxes
您是否尝试使用默认按钮将控件包装在面板中?我知道它适用于文本框
<asp:Panel runat="server" id="searchPanel" DefaultButton="btnGoButton">
<asp:DropDownList ID="ddlMenu" runat="server" />
<asp:Button ID="btnGoButton" runat="server" onclick="GoButton_Click"/>
</asp:Panel>
#2
Well, you are using the wrong button id.
好吧,你使用的是错误的按钮ID。
The ID in the __doPostBack method must be the UniqueID of the trigger control. You say the trigger is 'GoButton', but it has the ID 'btnGoButton'.
__doPostBack方法中的ID必须是触发器控件的UniqueID。你说触发器是'GoButton',但它有ID'btnGoButton'。
There is no way for the server to know that your 'GoButton' actually is the btnGoButton.
服务器无法知道你的'GoButton'实际上是btnGoButton。
Also remember that name mangling (a method to ensure unique names for controls) may mess up the UniqueID even more.
还要记住,名称修改(一种确保控件的唯一名称的方法)可能会使UniqueID更加混乱。
Try to write like this instead: __doPostBack('<%# btnGoButton.UniqueID %>', '');
尝试这样写:__ doPostBack('<%#btnGoButton.UniqueID%>','');
or append it in the codebehind..
或将其附加在代码隐藏中..
ddlMenu.Attributes.Add("onkeypress", string.Format("__doPostBack('{0}', '');", btnGoButton.UniqueID);
ddlMenu.Attributes.Add(“onkeypress”,string.Format(“__ doPostBack('{0}','');”,btnGoButton.UniqueID);