我有一个更新面板中的母版:
< ASP:UpdatePanel的ID =的UpdatePanel=服务器ChildrenAsTriggers =真的EnableViewState =假
的UpdateMode =有条件>
<的ContentTemplate>
< DIV ID =日程地址搜索Maincontent>
< ASP:的ContentPlaceHolder ID =ContentPlaceHolder1=服务器>
< / ASP:的ContentPlaceHolder>
< / DIV>
< /的ContentTemplate>
<触发器>
< /触发器>
< / ASP:UpdatePanel的>
然后,我有Default.aspx页面,它使用了母版文件:
< ASP:内容ID =内容1=服务器ContentPlaceHolderID =ContentPlaceHolder1>
< ASP:占位符ID =plhCurrentItem=服务器>< / ASP:占位符>
< / ASP:内容>
我编程方式加载用户控件与ID plhCurrentItem的占位符。
现在的问题是,当我点击在该用户的按钮时,没有任何事件触发。用户控件刚刚消失,在UpdatePanel是空白。
我是什么做错了吗?
更新
code用来添加用户控件。该LoadControls方法是从Page_Load事件调用。
控制ctlCurrentItem = NULL;
公共字符串currentControl
{
{返回((串)会议[currentControl]); }
集合{会议[currentControl] =值; }
}
公共无效LoadControls()
{
开关(currentControl)
{
情况下家庭:
ctlCurrentItem = Page.LoadControl(〜/页/ Home.ascx);
ctlCurrentItem.ID =家;
打破;
案简历:
ctlCurrentItem = Page.LoadControl(〜/页/ Resume.ascx);
ctlCurrentItem.ID =恢复;
打破;
案工程:
ctlCurrentItem = Page.LoadControl(〜/页/ Projects.ascx);
ctlCurrentItem.ID =项目;
打破;
案联系:
ctlCurrentItem = Page.LoadControl(〜/页/ Contact.ascx);
ctlCurrentItem.ID =联系;
打破;
默认:
返回;
}
plhCurrentItem.Controls.Clear();
plhCurrentItem.Controls.Add(ctlCurrentItem);
}
解决方案
将LoadControls请致电的页面生命周期:
使用此事件的以下内容:
检查IsPostBack属性,以确定这是否是第一次页面正在处理中。 创建或重新创建动态控件。 设置一个母版页动态。 主题属性动态设置。 在读取或设置配置文件属性值。I have a masterpage with an update panel:
<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="true" EnableViewState="False"
UpdateMode="Conditional">
<ContentTemplate>
<div id="mainContent">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
Then I have Default.aspx page which uses the masterpage file:
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:PlaceHolder ID="plhCurrentItem" runat="server"></asp:PlaceHolder>
</asp:Content>
I programmatically load usercontrols into the placeholder with id plhCurrentItem.
The problem is when I click a button in the usercontrol, no event fires. The usercontrol just disappears and the updatepanel is left blank.
What am I doing wrong?
Update
Code used to add usercontrols. The LoadControls method is called from the Page_load event.
Control ctlCurrentItem = null;
public string currentControl
{
get { return ((string)Session["currentControl"]); }
set { Session["currentControl"] = value; }
}
public void LoadControls()
{
switch (currentControl)
{
case "Home":
ctlCurrentItem = Page.LoadControl("~/pages/Home.ascx");
ctlCurrentItem.ID = "Home";
break;
case "Resume":
ctlCurrentItem = Page.LoadControl("~/pages/Resume.ascx");
ctlCurrentItem.ID = "Resume";
break;
case "Projects":
ctlCurrentItem = Page.LoadControl("~/pages/Projects.ascx");
ctlCurrentItem.ID = "Projects";
break;
case "Contact":
ctlCurrentItem = Page.LoadControl("~/pages/Contact.ascx");
ctlCurrentItem.ID = "Contact";
break;
default:
return;
}
plhCurrentItem.Controls.Clear();
plhCurrentItem.Controls.Add(ctlCurrentItem);
}
解决方案
Put LoadControls call in the OnPreInt event from the page life-cycle:
Use this event for the following:
Check the IsPostBack property to determine whether this is the first time the page is being processed. Create or re-create dynamic controls. Set a master page dynamically. Set the Theme property dynamically. Read or set profile property values.