【发布时间】:2014-06-26 23:08:20
【问题描述】:
我已经查看了几个similar questions 到这个,现在我正在尝试使用 FindControl 方法但没有成功。
我的 Web 表单应用程序有几个母版页。所有母版页的共同点是顶部导航用户控件中的搜索框和按钮。我正在尝试获取在 SearchResults 页面上输入的搜索词:
HtmlGenericControl topNavDiv = (HtmlGenericControl)Master.FindControl("topNavDiv");
Control topNav = (UserControl)Page.FindControl("topNav");
if (topNav != null)
{
TextBox searchBox = topNav.FindControl("searchBox") as TextBox;
if (searchBox != null)
{
Response.Write(searchBox.Text.Trim());
}
else
{
resultsPanel.Visible = false;
messagePanel.Visible = true;
}
}
在第一行,topNavDiv 变量为空。我正在考虑通过母版页属性访问用户控件,但是 对于几个不同的母版页,我不知道如何确定母版页的 ID,因为可以从站点中的任何位置启动搜索...
更新:
我能够抓取topNav div,searchBox文本框如下:
Control topNav = (UserControl)Master.FindControl("topNav");
if (topNav != null)
{
TextBox searchBox = topNav.FindControl("searchBox") as TextBox;
if (!String.IsNullOrEmpty(searchBox.Text))
{
Response.Write(searchBox.Text.Trim());
}
else
{
resultsPanel.Visible = false;
messagePanel.Visible = true;
}
}
唯一的问题是输入的搜索框文本没有被持久化。
【问题讨论】:
-
不使用嵌套母版页...
标签: asp.net webforms user-controls master-pages