此代码以前工作正常,基本上我有一个主页面,其中有一个用于搜索的文本框,我将其命名为 searchBox
。我有一种方法可以在表单提交中提取 searchBox
的内容,并将其设置为变量 userQuery
。这是方法:
Public Function searchString(ByVal oTextBoxName As String) As String
If Master IsNot Nothing Then
Dim txtBoxSrc As New TextBox
txtBoxSrc = CType(Master.FindControl(oTextBoxName), TextBox)
If txtBoxSrc IsNot Nothing Then
Return txtBoxSrc.Text
End If
End If
Return Nothing
End Function
结果显示在 search.aspx
上。但是,现在,如果在 searchBox
以外的页面上填写并提交 search.aspx
,则不会传递文本框的内容。表单非常简单,只需:
<asp:TextBox ID="searchBox" runat="server"></asp:TextBox>
。
<asp:Button ID="searchbutton" runat="server" Text="search" UseSubmitBehavior="True" PostBackUrl="~/search.aspx" CssClass="searchBtn" />
答案 0 :(得分:1)
我认为因为您使用的是PostBackUrl,所以您需要使用“PreviousPage”标识符来引用您的变量。
另一个解决方案是不使用PostBackUrl属性并捕获用户控件中的事件(我假设你将它封装在一个位置),然后使用:
Response.Redirect("/search.aspx?sQuery=" & Server.URLEncode(searchBox.Text))
因为您不一定要传递敏感数据,所以这也是可以接受的。
答案 1 :(得分:1)
我同意Kyle关于为什么它不起作用以及解决方案,如果你想继续通过文本控件访问该值,但你也可以从httprequest中获取表单数据。我想是这样的(我的asp.net有点生疏)
Request.Form[txtBoxSrc.UniqueID]
这里加上其他技术(使用previouspage属性)记录在此处:http://msdn.microsoft.com/en-us/library/6c3yckfw(VS.80).aspx。看来你需要做的就是:
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
return SourceTextBox.Text;
}
}
更新:感谢Jason Kealey指出我需要使用UniqueID。