Click here to Skip to main content
15,035,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have added usercontrol dynamically in my code. I have also added it on pre init state to handle its events. My user control contains one grid view. I can able to perform edit and delete operations on grid view. This user control is bind to treeview Node changed. Means It will show appropriate selected node information in the grid. But my prblm is when i am going to click on another link of treeview it will show me both ouputs...the new treeview link click information and previous treeview click information.
my code is as follows
C#
  #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;
  }

how to handle such situations
Posted
Updated 2-Dec-14 18:43pm
v2
Comments
   
Not clear what you problem is. If you want to clear something, add some Clear() method...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900