我有一个aspx页面,它继承自一个母版页并有两个内容占位符,如下所示:
<%@ Register Src="Controls/YearBook/YearBookExpander.ascx" TagName="YearBookExpander" TagPrefix="uc1" %>
<%@ Register Src="Controls/YearBook/YearBook.ascx" TagName="YearBook" TagPrefix="uc2" %>
<%@ Register Src="Controls/YearBook/YearBookSearch.ascx" TagName="YearBookSearch" TagPrefix="uc3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphHead" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMenu" runat="Server">
<asp:UpdatePanel ID="upExpander" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<nav id='main-nav'>
<asp:Panel ID="pnlExpander" runat="server" CssClass="navigation">
<uc1:YearBookExpander ID="ybExpander" runat="server" OnSearchClickEvent="ybExpander_Search" />
</asp:Panel>
</nav>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cphPage" runat="Server">
<asp:Panel ID="pnlPage" runat="server">
<uc2:YearBook ID="YearBook1" runat="server" ContentVisible="true" />
<uc3:YearBookSearch ID="ybSearch" runat="server" />
</asp:Panel>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="cphScripts" runat="Server">
<script src="../Scripts/PersianCalendar.js" type="text/javascript"></script>
<script type="text/javascript">
Calendar = 1;
</script>
</asp:Content>
出于某些javascript原因我不能把我的&#34; cphPage&#34;内容进入更新面板,我需要根据cphMenu中点击的链接显示/取消显示cphPage的用户控件。 为此,我将面板添加到usercontrol的更新面板中,并希望在发回消息时可见或不可见此面板,因此我在每个用户控件中定义一个属性(在cphPage内容占位符中):
[Bindable(true, BindingDirection.TwoWay)]
public bool ContentVisible
{
get { return Convert.ToBoolean(Session["ContentVisible"]); }
set { Session["ContentVisible"] = value; }
}
并且在更改值基础后面的aspx代码中单击了一个链接:
private void uExpander_ExpanderClicked(object sender, ExpanderEventArgs e)
{
switch (e.Clicked)
{
case "YearBook":
YearBook1.ContentVisible = true;
ybSearch.ContentVisible = false;
break;
case "Search":
ybSearch.ContentVisible = true;
YearBook1.ContentVisible = false;
break;
default:
YearBook1.ContentVisible = true;
ybSearch.ContentVisible = false;
break;
}
}
在用户控件中我将此值检查为可见/不可见的usercontrol面板:
if (ContentVisible)
{
pnlCalendar.Visible = true;
pnlDefaultEvents.Visible = true;
pnlAddEvent.Visible = true;
}
else
{
pnlCalendar.Visible = false;
pnlDefaultEvents.Visible = false;
pnlAddEvent.Visible = false;
}
但是这段代码不起作用,因为用户控件在执行aspx代码之后启动... 我该怎么做这个场景?还有什么其他解决方案吗?
谢谢
答案 0 :(得分:1)
您有两种选择:
将ContentVisible属性更改为:
[Bindable(true, BindingDirection.TwoWay)] public bool ContentVisible { set { pnlCalendar.Visible = value; pnlDefaultEvents.Visible = value; pnlAddEvent.Visible = value; } }
或移动该代码:
if (ContentVisible)
{ pnlCalendar.Visible = true; pnlDefaultEvents.Visible = true; pnlAddEvent.Visible = true; } else { pnlCalendar.Visible = false; pnlDefaultEvents.Visible = false; pnlAddEvent.Visible = false; }
进入生命周期后期的用户控件中的事件,而不是来自父页面的uExpander_ExpanderClicked事件。 OnPreRender应该迟到但不能太晚进行隐藏/显示面板。