Wednesday, March 21, 2012

Partial Page Render not working with Master page

I have a child page using partial page rendering and the web controls are created programmatically. Ajax seems to have a problem with control ID naming when you create your web controls dynamically. When I run this it gives me an object instance error indicating the named control "Lable1" doesn't exist.

Has anyone else run into this problem?

Is there a work around?

Master Page

<%@dotnet.itags.org.MasterLanguage="C#"AutoEventWireup="true"CodeFile="MasterPage.master.cs"Inherits="MasterPage" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:contentplaceholderid="ContentPlaceHolder1"runat="server">

</asp:contentplaceholder>

</div>

</form>

</body>

</html>

CS child page

<%@dotnet.itags.org.PageLanguage="C#"MasterPageFile="~/MasterPage.master"Title="Untitled Page" %>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">

<scriptrunat="server">

protectedvoid Page_Load(object sender,EventArgs e)

{

UpdatePanel up1 =newUpdatePanel();

up1.ID ="UpdatePanel1";

up1.UpdateMode =UpdatePanelUpdateMode.Conditional;

Button button1 =newButton();

button1.ID ="Button1";

button1.Text ="Submit";

button1.Click +=newEventHandler(Button_Click);

Label label1 =newLabel();

label1.ID ="Label1";label1.Text ="A full page postback occurred.";

up1.ContentTemplateContainer.Controls.Add(button1);

up1.ContentTemplateContainer.Controls.Add(label1);

Page.Form.Controls.Add(up1);

}

protectedvoid Button_Click(object sender,EventArgs e)

{

((Label)Page.FindControl("Label1")).Text ="Panel refreshed at " +DateTime.Now.ToString();

}

</script>

<div>

<asp:ScriptManagerID="ScriptManager1"runat="server">

</asp:ScriptManager>

</div>

</asp:Content>

Create your controls in the Page_Init vs the Page_Load method.

-Damien


Damien -

I tried creating the controls in the Page_Init and it still will not work. The referrence to the control is still null. Is the page lifecycle timing wrong with the Button_Click event?


Try refering your control with:

YourUpdatePanel.ContentTemplateContainer.FindControl("YourControlName")


I don't have access to the UpdatePanel at design time.


UpdatePanel.ContentTemplateContainer.FindControl("controlName")


Thank you. Seems to work referencing the UpdatePanel "up1". Although you must instantiate "up1" as global. Simple but necessary.

No comments:

Post a Comment