Showing posts with label content. Show all posts
Showing posts with label content. Show all posts

Monday, March 26, 2012

Instantiate Accordion content

Hi,

I have an accordion with some panes that have custom controls in their content templates.

The accordion is NOT databound ... the contents are static, though viewstate is helping keep track of the values in the custom controls.

When the page is created for the very first time, NOT postback, the accordion panes do not instantiate the user controls until sometime during the OnPreRender stage in the lifecycle.

On subsequent postbacks however, the accordion panes do instantiate their controls during the LoadState stage, due to their presence in ViewState.

How can I make the accordion panes instantiate their content templates earlier than OnPreRender when the page is created for the first time?

The accordion pane's content templates are not dynamic. They are coded into the page markup.

I have tried calling something like Accordion.Panes[0].Content.InstantiateIn(Accordion.Panes[0]) and various combinations at various stages, but I usually ended up just getting duplicate versions of the controls in the wrong places in the page!

I could not find any "EnsureControls()" methods available in the accordion.

Many thanks in advance for your helpful suggestions,

Ben

Hi,
Accordion.Panes[0].FindControl('nothing') will make pane 0 to instantiate its templates. FindControl is an indirect way to call EnsureChildControls.

-yuriy
http://couldbedone.blogspot.com


Yuriy! Thank you so much! I'm relieved!

Wednesday, March 21, 2012

Is it possible to add a ScriptManager and/or UpdatePanel to a MasterPage from a ContentPag

I would on like AJAX on one content page and want to add an UpdatePanel to the MasterPage and wrap a UserControl that is being loaded from the MasterPage in that UpdatePanel all from the ContentPage. Is it possbile to do this? I have some code but it is not working.

protected override void OnPreInit( EventArgs e ) { ScriptManager sm =new ScriptManager(); sm.ID ="ScriptManager1"; Master.Controls.Add( sm ); UpdatePanel panel =new UpdatePanel(); panel.ID ="upMaster"; panel.ContentTemplateContainer.Controls.Add(this.Controls[0] ); AsyncPostBackTrigger trig =new AsyncPostBackTrigger(); trig.ControlID ="btnDelete"; trig.EventName ="Click"; panel.Triggers.Add( trig ); Master.Controls.Add( panel );base.OnPreInit( e ); }

I changed my code and now can get the page to load but now I dont see my usercontrol on the masterpage

protected override void OnPreInit( EventArgs e ) { UpdatePanel panel =new UpdatePanel(); panel.ID ="upMaster"; UserControl uc = (( UserControl )Master.FindControl("CartSummary1" )); panel.ContentTemplateContainer.Controls.Add( uc ); panel. //AsyncPostBackTrigger trig =new AsyncPostBackTrigger();//trig.ControlID = "btnDelete"; //trig.EventName = "Click"; //panel.Triggers.Add( trig ); HtmlForm form1 = ( HtmlForm )Master.FindControl("Form1" ); form1.Controls.Add( panel );//Master.Controls.Add( panel );base.OnPreInit( e ); }

Yes,It is possible.

Do it like this:

MasterPage.master:

<%@. Master Language="C#" %>
<%@. Register src="http://pics.10026.com/?src=UserControlA.ascx" TagName="UserControlA" TagPrefix="uc1" %>

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

<script runat="server">

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:UserControlA ID="uc1" runat="server" />
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
<asp:contentplaceholder id="ContentPlaceHolder2" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

Default.aspx:

<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString();
}


protected void Page_PreInit(object sender, EventArgs e)
{
HtmlForm form1 = (HtmlForm)Master.FindControl("form1");
ScriptManager sm = new ScriptManager();
sm.ID = "ScriptManager1";
form1.Controls.AddAt(0, sm);

UpdatePanel panel = new UpdatePanel();
panel.ID = "upMaster";
UserControl uc = ((UserControl)Master.FindControl("uc1"));
panel.ContentTemplateContainer.Controls.Add(uc);
form1.Controls.Add(panel);
}

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div style="display:none;"><asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button1_Click" /></div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<script type="text/javascript" language="javascript">
function doclick(){
$get('<%= Button2.ClientID%>').click();
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="false" OnClientClick="doclick();return false;" />
</asp:Content>

UserControlA.ascx:

<%@. Control Language="C#" ClassName="UserControlA" %>

<asp:TextBox ID="TextBox1" runat="server">TextBox in UserControlA</asp:TextBox>

Best Regards,