response.redirect 无法在带有 page_load 的页面上工作

发布于 2024-12-04 23:03:15 字数 1153 浏览 1 评论 0原文

我在母版页上有一个文本框,我将其用作搜索框。当用户按下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-12-11 23:03:15

您可以避免整个访问服务器和重定向的过程。你可以简单地这样做:

function do_search() {
        if (event.keyCode == 13) {
          var textbox = document.getElementById('<%=txtSearch.ClientID%>');
          if(textbox!=null)
             window.location('viewall.aspx?q='+textbox.value);
        }
    }

You can avoid the whole going to the server and redirecting. You can simply do this:

function do_search() {
        if (event.keyCode == 13) {
          var textbox = document.getElementById('<%=txtSearch.ClientID%>');
          if(textbox!=null)
             window.location('viewall.aspx?q='+textbox.value);
        }
    }
相权↑美人 2024-12-11 23:03:15

好的,感谢上面的回答,但似乎不起作用。我终于通过这篇文章解决了这个问题......这似乎是一个神秘的问题,并且与浏览器相关。

http://www.pcreview .co.uk/forums/response-redirect-not-working-pressing-enter-key-t2888253.html

第三篇文章:

“然后我尝试包装控件在一个
面板并设置面板的DefaultButton,这似乎得到了它
在 IE 中工作。”

我的页面现在如下:

                <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSearch">
                    <asp:TextBox ID="txtSearch" runat="server" Text=""></asp:TextBox>
                    <asp:Button ID="btnSearch" runat="server" Style="display: none" />
                </asp:Panel>

...它终于可以工作了!

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:

                <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSearch">
                    <asp:TextBox ID="txtSearch" runat="server" Text=""></asp:TextBox>
                    <asp:Button ID="btnSearch" runat="server" Style="display: none" />
                </asp:Panel>

...and it works! finally.

薆情海 2024-12-11 23:03:15

您可以尝试使用 RaisePostBackEvent 方法,每当使用 __doPostBack 时都会调用该方法。

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

    if (source == btnSearch)
    {
        Response.Redirect("...");
    }
}

You can try using the RaisePostBackEvent method, which is called whenever __doPostBack is used.

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

    if (source == btnSearch)
    {
        Response.Redirect("...");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文