#region Maintain User Control protected Control GetPostBackControl(Page page) { Control control = null; try { string ctrlName = page.Request.Params.Get("__EVENTTARGET"); if (ctrlName != null && ctrlName != String.Empty) { control = page.FindControl(ctrlName); } else { ContentPlaceHolder cph = (ContentPlaceHolder)page.FindControl("Main"); for (int i = 0, len = page.Request.Form.Count; i < len; i++) { string[] ctl = page.Request.Form.AllKeys[i].Split('$'); if (ctl.Length > 2) { control = cph.FindControl(ctl[2]) as System.Web.UI.WebControls.Button; } if (control != null) break; } } } catch (Exception ex) { throw ex; } return control; } protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); Control control = GetPostBackControl(this.Page); // Check if the postback is caused by the button // Titled "Click to Create a Dynamic Control" // OR // createAgain field is true // which means a call is made to the server while the // user control is active if (_flag==1) { _flag = 0; UpdateSite2.ContentTemplateContainer.Controls.Clear(); } else { if ((control != null && control.ClientID == PID.ToString()) || createAgain) { //should be set before the CreateUserControl method createAgain = true; UpdateSite2.ContentTemplateContainer.Controls.Clear(); CreateUserControl(PID.ToString()); } } } protected void CreateUserControl(string controlID) { // createAgain field is set to true in the OnPreInit method // when the 'Create User Control' button is clicked // the createAgain field is used to check if the // user control is on the page before the call // if so create the control again and add it to the // Control Hierarchy again try { if (createAgain != null)//&& PlaceHolder1 { if (Session.Count > 0) { UpdateSite2.ContentTemplateContainer.Controls.Clear(); for (int i = 0; i < Session.Count; i++) { switch (Session[i].ToString()) { case "ASP.usercontrols_site_siteuc_ascx": { // Create instance of the UserControl SimpleControl UserControls_Site_SiteUC ucSimpleControl = LoadControl("~/UserControls/Site/SiteUC.ascx") as UserControls_Site_SiteUC; // Set the Public Properties ucSimpleControl.SiteID = ((UserControls_Site_SiteUC)(Session[i])).SiteID; //ucSimpleControl.LastName.Text = ((UserControls_Site_SiteUC)(Session[i])).LastName.Text; //Create Event Handler for btnPost Click //ucSimpleControl.btnPostClk += new btnPost_Click(ucSimpleControl_btnPostClk); //Add the SimpleControl to Placeholder UpdateSite2.ContentTemplateContainer.Controls.Add(ucSimpleControl); break; } } } } } } catch (Exception ex) { throw ex; } } #endregion if (TreeView1.SelectedNode.ToolTip == "SiteName") { UserControls_Site_SiteUC usercntl = (UserControls_Site_SiteUC)Page.LoadControl("~/UserControls/Site/SiteUC.ascx"); usercntl.SiteID = int.Parse(PID.ToString()); UpdateSite2.ContentTemplateContainer.Controls.Add(usercntl); UpdateSite2.Visible = true; SitePanel.Visible = true; // Add the instance of the SimpleControl to Session Variable Session.Add((Session.Count+1).ToString(), usercntl); // Set createAgain = true createAgain = true; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)