How to fix the dynamically added user control being called more than 1 at page prerender event
<p>i am facing  a weird problem, i were to write some UserControl on fly, on some button click, so on that button click i chose the user control required to add </p>

<p>dynamically, i succeeded doing this but then the problem i faced to persist the user control that i was loosing on post back, so i managed it by checking viewstate at </p>

<p>page load, it was working fine but later i stuck with one more issue when the user control was still rendering when i cleared the view state for that user control on </p>

<p>some dropdown selected index change, so i figured out that the page load occurring before the drop-down cler the viewstate, so i resisted a PreRender event on the page </p>

<p>there i am dynamically loading the usercontrol, but now the problem i am facing is that, the user control getting written for 3 time, through break point i see that </p>

<p>the page_PreRender event getting called 3 times, i am unable to fix it, please help me with this.</p>

<p>wring the partial code.</p>

<p>Follwing i do on button click event</p>

<pre><code>if (e.CommandName == "Add")
        {
            ViewState["addControl"] = true;
            isAddNew = true;

            divSummary.Visible = false;
            pnlDetails.Visible = true;
        }
</code></pre>

<p>this is how user control is loading</p>

<pre><code>     protected void fillContentDetail()
        {
            int ContentTypeID = int.Parse(ViewState["ContentTypeID"].ToString());
            //string sqlQuery = "SELECT ContentTypeID FROM tblSiteContentLists WHERE SiteContentListID = " + ddlList.SelectedValue.ToString();
            //SqlParameter[] arrParams = new SqlParameter[0];
            //DataTable dt = Data.GetSQLQueryResults(sqlQuery, 0, arrParams);
            //int ContentTypeID = int.Parse(dt.Rows[0][0].ToString());
            //ViewState["ContentTypeID"] = ContentTypeID;
            switch (ContentTypeID)
            {
                case 1:
                    myCtrl = Page.LoadControl("~/Controls/DocumentSpreadSheet.ascx");

                    if (myCtrl != null)
                    {
                        ((Controls_DocumentSpreadSheet)myCtrl).OnItemSave += new Controls_DocumentSpreadSheet.ItemSaved(ctrl_OnItemSave);
                        ((Controls_DocumentSpreadSheet)myCtrl).ddlList = int.Parse(ddlList.SelectedValue);
                        ((Controls_DocumentSpreadSheet)myCtrl).hdnContentTypeID = hdnContentTypeID.Value.ToString();
                        ((Controls_DocumentSpreadSheet)myCtrl).userAccountID = (ViewState["UserAccountID"] == null) ? -1 : int.Parse(ViewState

["UserAccountID"].ToString());// string.IsNullOrEmpty(hdnUserAccountID.Value.ToString()) ? -1 : int.Parse(hdnUserAccountID.Value.ToString());
                        if (ViewState["isEdit"] != null)
                        {
                            if ((bool)ViewState["isEdit"])
                            {
                                ((Controls_DocumentSpreadSheet)myCtrl).hdnContentID = int.Parse(hdnContentID.Value.ToString());
                            }
                        }
                        pnlDetails.Controls.Add(myCtrl);
                    }
                    break;
</code></pre>

<p>this is my prerender event that takes action accordingly.</p>

<pre><code> private void Page_PreRender(object sender, System.EventArgs e)
    {
        if (ViewState["addControl"] != null)
        {
            if ((bool)ViewState["addControl"])
            {
                fillContentDetail();
            }
        }
        if (ViewState["isEdit"] != null)
        {
            if ((bool)ViewState["isEdit"])
            {
                fillContentDetail();
            }
        }
</code></pre>

<p>}</p>
