【发布时间】:2011-09-13 20:33:06
【问题描述】:
我在母版页上有一个文本框,用作搜索框。当用户按下回车键时,我想使用 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>
在母版页页面加载:
txtSearch.Attributes.Add("onKeyDown", "do_search(this)")
javascript 函数,当用户按下回车键时,它会调用 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 的页面上触发。
有什么想法吗?
【问题讨论】: