ASP.NET 默认按钮
我有一个主页,顶部有一个搜索框和按钮。 此搜索功能正在接管使用此母版页的所有 Web 表单的“输入”键。 也就是说,如果我有一个使用此母版页的登录页面,并且用户输入用户名/密码并点击“回车”,系统将执行搜索,而不是登录用户。
设置默认提交按钮的最佳方法是什么? 我可以将所有内容包装在 asp 面板中并设置 DefaultButton 属性,但这似乎有点乏味。 有没有更好的办法?
I have a master page with a search box and button at the top. This search functionality is taking over the "enter" key for all my web forms that use this master page. That is, if I have a login page that uses this master page and the user enters in their username/password and hits "enter", instead of logging in the user the system performs a search.
What's the best way to set up the default submit buttons. I could wrap everything in asp Panels and set the DefaultButton property, but that seems a bit tedious. Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用表单或面板的defaultbutton属性
use defaultbutton property of form or panel
正如您所发现的,当页面上有多个按钮时,默认按钮可能会导致问题。 我会采纳极客的建议,但通过删除 setfocus 客户端脚本来简化它,并通过将 keydown 事件添加到搜索文本框和登录文本框来扩展它,这样回车键就会根据您的用户是否使用搜索来触发正确的按钮框或登录框,或您想要添加 JavaScript 的任何其他文本框。
您可以将焦点设置为加载页面(如果您愿意,可以在代码后面),以节省用户的一些鼠标工作或选项卡(如果有一个合理的控件供用户开始),但否则用户与之交互的控件应确定页面的流程以及回车键的作用。
As you have discovered default buttons can cause issues when there is more than one button on a page. I would take the Geeks suggestion but simplify it by removing the setfocus client script and extend it by adding the keydown event to both the search textbox and the login textboxes such that the enter key fires the correct button depending on if your user is using the search box or the log in box, or any other textbox you want to add the javascript to.
You can set focus on loading the page (in code behind if you like) to save the user some mouse work or tabbing if there is a sensible control for the user to start at, but otherwise the control the user is interacting with should determine the flow of the page and what the enter key does.
将焦点设置在文本框上,
Page.RegisterStartupScript("SetFocus", "< script > document.getElementById('" + TextBox1.ClientID + "').focus();< /script >");
然后
在最后一个文本框的 keydown 事件中,您可以执行以下操作:
If e.KeyCode = Keys.Enter then
Me.Button1.Select()
万一
set focus on the text box ,
Page.RegisterStartupScript("SetFocus", "< script >document.getElementById('" + TextBox1.ClientID + "').focus();< /script >");
and then
In the keydown event for the last textbox you can do something like this:
If e.KeyCode = Keys.Enter Then
Me.Button1.Select()
End If
您可以将搜索框和提交按钮放入母版页上的 IFRAME 中,而不是使用 javascript 操作页面。 如果焦点位于 iframe 中,单击将提交搜索表单,如果用户位于页面内的主表单上,他们将提交普通页面。
src 属性指向一个小型自包含 aspx 页面,其中包含文本框和重定向到搜索结果表单的提交按钮。
Rather than using javascript to manipulate the page you could put the search box and the submit button into an IFRAME on the master page. If the focus is in the iframe clicking will submit the search form, if the user is on your main form within the page they will submit the normal page.
The
<iframe>
src attribute points to a little self contained aspx page holding your text box and submit button which redirects to your search results form.这是我们在项目中尝试过的方法,似乎有效。 我将搜索 asp:Button 更改为 asp:LinkButton。 然后我添加了一些 CSS 样式来为其提供背景图像,使其看起来像一个按钮。 页面显然没有使用 LinkButtons 来确定按 Enter 键时哪个按钮是默认操作。 最棒的是 LinkButton 仍然可以具有与其关联的单击操作,因此我不必更改任何代码隐藏内容。
This is what we tried on our project which seemed to work. I changed the Search asp:Button to be an asp:LinkButton. I then added some CSS style to give it a background image to make it look like a button. LinkButtons are apparently not used by the page for determining which Button is the default action when pressing Enter. The sweet part was that LinkButton can still have click actions associated with them so I didn't have to change any of my code-behind stuff.