|
| 1 | +--- |
| 2 | +title: Initiating Requests from one ContentPlaceHolder and Loading/Updating Controls in another |
| 3 | +page_title: Initiating Requests from one ContentPlaceHolder and Loading/Updating Controls in another | RadAjax for ASP.NET AJAX Documentation |
| 4 | +description: Initiating Requests from one ContentPlaceHolder and Loading/Updating Controls in another |
| 5 | +slug: ajax/radajaxmanager/how-to/initiating-requests-from-contentplaceholder |
| 6 | +tags: radajax,and,masterpage |
| 7 | +published: True |
| 8 | +position: 4 |
| 9 | +--- |
| 10 | + |
| 11 | +# Initiating Requests from one ContentPlaceHolder and Loading/Updating Controls in another |
| 12 | + |
| 13 | + |
| 14 | +**RadAjaxManager** could handle all the **MasterPage** and **WebUserControls** scenarios no matter the complexity of the application. To prove this, we show you how it can handle a scenario, where loading of WebUserControls is done in contents of MasterPage by RadAjaxManager. |
| 15 | + |
| 16 | +An important point to notice in the following implementation, is the event bubbling approach. We use it because we load dynamically a user control from an action in another loaded user control. So the button click event is bubbled from the user control to the content page, where the first user control is loaded on each page load. Then we determine whether to perform loading of the second user control depending on the command from the button's bubbled event. |
| 17 | + |
| 18 | +Here is the full working code containing a MasterPage, content page and two simple WebUserControls: |
| 19 | + |
| 20 | +````ASP.NET |
| 21 | +<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" /> |
| 22 | +<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" /> |
| 23 | +<asp:Panel ID="Panel1" runat="server"> |
| 24 | + <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server" /> |
| 25 | +</asp:Panel> |
| 26 | +```` |
| 27 | + |
| 28 | +This _MasterPage_ has nothing in its code-behind. |
| 29 | + |
| 30 | +Standard content page **ASPX**: |
| 31 | + |
| 32 | +````ASP.NET |
| 33 | +<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server" /> |
| 34 | +<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server" /> |
| 35 | +```` |
| 36 | + |
| 37 | +The key **content page code-behind**: |
| 38 | + |
| 39 | +````C# |
| 40 | +private Control Content(string id) |
| 41 | +{ |
| 42 | + return Page.Master.FindControl(id); |
| 43 | +} |
| 44 | +protected void Page_Load(object sender, System.EventArgs e) |
| 45 | +{ |
| 46 | + //Load |
| 47 | + Content("ContentPlaceHolder1").Controls.Add(LoadControl("~/Page1.ascx")); |
| 48 | + Content("ContentPlaceHolder1").Controls[1].ID = "UserControl1"; |
| 49 | + //Get loaded user control's buttons |
| 50 | + Button AjaxButtonLoad = (Button)Content("ContentPlaceHolder1").FindControl("UserControl1").FindControl("btnLoad"); |
| 51 | + Button AjaxButtonClear = (Button)Content("ContentPlaceHolder1").FindControl("UserControl1").FindControl("btnClear"); |
| 52 | + //Get the manager and the panel from the MasterPage |
| 53 | + RadAjaxManager AjaxManager = RadAjaxManager.GetCurrent(Page); |
| 54 | + Panel MyPanel = (Panel)Content("Panel1"); |
| 55 | + //Add the necessary AJAX settings - buttons update the panel in order to load the second user control there |
| 56 | + AjaxManager.AjaxSettings.AddAjaxSetting(AjaxButtonLoad, MyPanel, null); |
| 57 | + AjaxManager.AjaxSettings.AddAjaxSetting(AjaxButtonClear, MyPanel, null); |
| 58 | +} |
| 59 | +protected override bool OnBubbleEvent(object source, System.EventArgs args) |
| 60 | +{ |
| 61 | + CommandEventArgs commandArgs = args as CommandEventArgs; |
| 62 | + //determine whether to load the user control depending on bubbled button command |
| 63 | + if (commandArgs != null) |
| 64 | + { |
| 65 | + if (commandArgs.CommandName == "Load") |
| 66 | + { |
| 67 | + Content("ContentPlaceHolder2").Controls.Add(LoadControl("~/Page2.ascx")); |
| 68 | + Content("ContentPlaceHolder2").Controls[1].ID = "UserControl2"; |
| 69 | + } |
| 70 | + else if (commandArgs.CommandName == "Clear") |
| 71 | + { |
| 72 | + //do nothing. the control won't be loaded anyway |
| 73 | + } |
| 74 | + } |
| 75 | + return base.OnBubbleEvent(source, args); |
| 76 | +} |
| 77 | +```` |
| 78 | +````VB |
| 79 | +Private Function Content(ByVal id As String) As Control |
| 80 | + Return Page.Master.FindControl(id) |
| 81 | +End Function |
| 82 | + |
| 83 | +Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load |
| 84 | + 'Load |
| 85 | + Content("ContentPlaceHolder1").Controls.Add(LoadControl("~/Page1.ascx")) |
| 86 | + Content("ContentPlaceHolder1").Controls(1).ID = "UserControl1" |
| 87 | + |
| 88 | + 'Get loaded user control's buttons |
| 89 | + Dim AjaxButtonLoad As Button = CType(Content("ContentPlaceHolder1").FindControl("UserControl1").FindControl("btnLoad"), Button) |
| 90 | + Dim AjaxButtonClear As Button = CType(Content("ContentPlaceHolder1").FindControl("UserControl1").FindControl("btnClear"), Button) |
| 91 | + |
| 92 | + 'Get the manager and the panel from the MasterPage |
| 93 | + Dim AjaxManager As RadAjaxManager = RadAjaxManager.GetCurrent(Page) |
| 94 | + Dim MyPanel As Panel = CType(Content("Panel1"), Panel) |
| 95 | + |
| 96 | + 'Add the necessary AJAX settings - buttons update the panel in order to load the second user control there |
| 97 | + AjaxManager.AjaxSettings.AddAjaxSetting(AjaxButtonLoad, MyPanel, Nothing) |
| 98 | + AjaxManager.AjaxSettings.AddAjaxSetting(AjaxButtonClear, MyPanel, Nothing) |
| 99 | +End Sub |
| 100 | + |
| 101 | +Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal args As System.EventArgs) As Boolean |
| 102 | + Dim commandArgs As CommandEventArgs = TryCast(args, CommandEventArgs) |
| 103 | + |
| 104 | + 'determine whether to load the user control depending on bubbled button command |
| 105 | + If commandArgs IsNot Nothing Then |
| 106 | + If commandArgs.CommandName = "Load" Then |
| 107 | + Content("ContentPlaceHolder2").Controls.Add(LoadControl("~/Page2.ascx")) |
| 108 | + Content("ContentPlaceHolder2").Controls(1).ID = "UserControl2" |
| 109 | + ElseIf commandArgs.CommandName = "Clear" Then |
| 110 | + 'do nothing. the control won't be loaded anyway |
| 111 | + End If |
| 112 | + End If |
| 113 | + |
| 114 | + Return MyBase.OnBubbleEvent(source, args) |
| 115 | +End Function |
| 116 | +```` |
| 117 | + |
| 118 | +First **WebUserControl's** code, which is loaded on each page load: |
| 119 | + |
| 120 | +````ASP.NET |
| 121 | +<asp:Button ID="btnLoad" runat="server" Text="Load" CommandName="Load" /> |
| 122 | +<asp:Button ID="btnClear" runat="server" Text="Clear" CommandName="Clear" /> |
| 123 | +```` |
| 124 | + |
| 125 | +**Code-behind**: |
| 126 | + |
| 127 | +````C# |
| 128 | +public event EventHandler BtnClick; |
| 129 | +protected void Button1_Click(object sender, System.EventArgs e) |
| 130 | +{ |
| 131 | + if (BtnClick != null) |
| 132 | + { |
| 133 | + BtnClick(sender, e); |
| 134 | + } |
| 135 | +} |
| 136 | +```` |
| 137 | +````VB |
| 138 | +Public Event BtnClick As EventHandler |
| 139 | + |
| 140 | +Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click |
| 141 | + RaiseEvent BtnClick(sender, e) |
| 142 | +End Sub |
| 143 | +```` |
| 144 | + |
| 145 | +And the second _WebUserControl_, which is loaded when the button _"Load"_ in the first _WebUserControl_ is clicked: |
| 146 | + |
| 147 | +It just shows some text in the ASPX and has nothing in its code-behind. |
| 148 | +````ASP.NET |
| 149 | +UserControl2 |
| 150 | +```` |
| 151 | + |
| 152 | + |
0 commit comments