response.redirect 无法在带有 page_load 的页面上工作
我在母版页上有一个文本框,我将其用作搜索框。当用户按下 Enter 键时,我想重定向到另一个页面,其中包含 url 参数中的搜索词。
麻烦的是,它似乎只适用于没有自己的 page_load 子项的页面。
<div id="search-bar">
<asp:TextBox ID="txtSearch" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" style="display:none"/>
</div>
IN masterpage page_load:
txtSearch.Attributes.Add("onKeyDown", "do_search(this)")
javascript 函数,以便当用户按下 Enter 时,它会调用 btnSearch_Click
function do_search() {
if (event.keyCode == 13) {
var invisibleButton = document.getElementById('<%=btnSearch.ClientID %>');
__doPostBack(invisibleButton.name, '');
}
}
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
If Trim(txtSearch.Text) <> "" Then
Response.Redirect("viewall.aspx?q=" & txtSearch.Text, True)
End If
End Sub
它仅适用于没有 Page_load 的页面,即,response.redirect 不会在具有 page_load 的页面上触发。
有什么想法吗?
I have a textbox onin a masterpage which I'm using as a search box. When the user presses enter I want to redirect to an other page with the search terms in the url paramaters.
Trouble it only seems to work on pages that don't have their own page_load subs.
<div id="search-bar">
<asp:TextBox ID="txtSearch" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" style="display:none"/>
</div>
IN masterpage page_load:
txtSearch.Attributes.Add("onKeyDown", "do_search(this)")
javascript function so that when user presses enter it calls btnSearch_Click
function do_search() {
if (event.keyCode == 13) {
var invisibleButton = document.getElementById('<%=btnSearch.ClientID %>');
__doPostBack(invisibleButton.name, '');
}
}
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
If Trim(txtSearch.Text) <> "" Then
Response.Redirect("viewall.aspx?q=" & txtSearch.Text, True)
End If
End Sub
It only works on pages that don't have a Page_load i.e. the response.redirect doesn't fire on pages with a page_load.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以避免整个访问服务器和重定向的过程。你可以简单地这样做:
You can avoid the whole going to the server and redirecting. You can simply do this:
好的,感谢上面的回答,但似乎不起作用。我终于通过这篇文章解决了这个问题......这似乎是一个神秘的问题,并且与浏览器相关。
http://www.pcreview .co.uk/forums/response-redirect-not-working-pressing-enter-key-t2888253.html
第三篇文章:
“然后我尝试包装控件在一个
面板并设置面板的DefaultButton,这似乎得到了它
在 IE 中工作。”
我的页面现在如下:
...它终于可以工作了!
Ok, thanks for the answers above but didn't seem to work. I finally got around this via this article...it seems a mysterious problem and is browser related.
http://www.pcreview.co.uk/forums/response-redirect-not-working-pressing-enter-key-t2888253.html
3rd post down:
"I then tried wrapping the controls in a
Panel and set the Panel's DefaultButton and that seemed to get it
working in IE."
My page now is as follows:
...and it works! finally.
您可以尝试使用 RaisePostBackEvent 方法,每当使用
__doPostBack
时都会调用该方法。You can try using the RaisePostBackEvent method, which is called whenever
__doPostBack
is used.