Is it possible to add a ScriptManager and/or UpdatePanel to a MasterPage from a ContentPage dynaically?

Last post 09-24-2007 5:16 AM by Jin-Yu Yin - MSFT. 2 replies.

Sort Posts:

  • Is it possible to add a ScriptManager and/or UpdatePanel to a MasterPage from a ContentPage dynaically?

    09-19-2007, 11:33 AM
    • Member
      5 point Member
    • nhaydon
    • Member since 07-20-2006, 2:18 PM
    • Posts 4

    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 );
        }
     
  • Re: Is it possible to add a ScriptManager and/or UpdatePanel to a MasterPage from a ContentPage dynaically?

    09-19-2007, 2:22 PM
    • Member
      5 point Member
    • nhaydon
    • Member since 07-20-2006, 2:18 PM
    • Posts 4

    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 );
        }
     
  • Re: Is it possible to add a ScriptManager and/or UpdatePanel to a MasterPage from a ContentPage dynaically?

    09-24-2007, 5:16 AM
    Answer

    Yes,It is possible.

    Do it like this:

    MasterPage.master:

    <%@ Master Language="C#" %>
    <%@ Register 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,

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
Page 1 of 1 (3 items)