Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8033917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:51:16+00:00 2026-06-05T01:51:16+00:00

I am dynamically adding a custom user control to an update panel. My user

  • 0

I am dynamically adding a custom user control to an update panel. My user control contains two dropdownlists and a textbox. When a control outside of the update panel triggers a postsback, I am re-adding the user control to the update panel.

The problem is…on postback when I re-add the user controls, it’s firing the “SelectedIndexChanged” event of the dropdownlists inside the user control. Even if the selectedindex did not change since the last postback.

Any ideas?

I can post the code if necessary, but there’s quite a bit in this particular scenario.

Thanks in advance!

EDIT…CODE ADDED BELOW

*.ASCX

<asp:DropDownList ID="ddlColumns" OnSelectedIndexChanged="ddlColumns_SelectedChanged" AppendDataBoundItems="true" AutoPostBack="true" runat="server">

*.ASCX.CS

List<dataColumnSpecs> dataColumns = new List<dataColumnSpecs>();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        fillDDLColumns();
    }        
}

public void fillDataColumnsList()
{
    dataColumns.Clear();
    //COMMON GETDATATABLE RETURNS A DATA TABLE POPULATED WITH THE RESULTS FROM THE STORED PROC COMMAND
    DataTable dt = common.getDataTable(storedProcs.SELECT_COLUMNS, new List<SqlParameter>());
    foreach (DataRow dr in dt.Rows)
    {
        dataColumns.Add(new dataColumnSpecs(dr["columnName"].ToString(), dr["friendlyName"].ToString(), dr["dataType"].ToString(), (int)dr["dataSize"]));
    }
}

public void fillDDLColumns()
{
    fillDataColumnsList();
    ddlColumns.Items.Clear();
    foreach (dataColumnSpecs dcs in dataColumns) 
    { 
        ListItem li = new ListItem(); 
        li.Text = dcs.friendlyName; 
        li.Value = dcs.columnName; 
        ddlColumns.Items.Add(li);
    }
    ddlColumns.Items.Insert(0, new ListItem(" -SELECT A COLUMN- ", ""));
    ddlColumns.DataBind();  
}

protected void ddlColumns_SelectedChanged(object sender, EventArgs e)
{
    //THIS CODE IS BEING FIRED WHEN A BUTTON ON THE PARENT *.ASPX IS CLICKED
}

*.ASPX

<asp:UpdatePanel ID="upControls" runat="server">
    <ContentTemplate>
        <asp:Button ID="btnAddControl" runat="server" Text="+" OnClick="btnAddControl_Click" />
    </ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" ValidationGroup="vgGo" />
<asp:GridView...

*.ASPX.CS

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        uc_Counter = 0;
        addControl();
        gridview_DataBind();
    }
    else
    {
        reloadControls();
    }
}

protected void btnGo_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        //THIS BUTTON CLICK IS WHAT'S TRIGGERING THE 
        //SELECTEDINDEXCHANGED EVENT TO FIRE ON MY *.ASCX
        gridview_DataBind();
    }   
}

private void reloadControls()
{
    int count = this.uc_Counter;

    for (int i = 0; i < count; i++)
    {
        Control myUserControl = Page.LoadControl("~/Controls/myUserControl.ascx");
        myUserControl.ID = "scID_" + i;
        upControls.ContentTemplateContainer.Controls.AddAt(i, myUserControl);
        ((customUserControl)myUserControl).fillDDLColumns();
    }
}

private void addControl()
{
    Control myUserControl = Page.LoadControl("~/Controls/myUserControl.ascx");
    myUserControl.ID = "scID_" + uc_Counter.ToString();
    upControls.ContentTemplateContainer.Controls.AddAt(upControls.ContentTemplateContainer.Controls.IndexOf(btnAddControl), myUserControl);            
    //((customUserControl)myUserControl).fillDDLColumns();
    this.uc_Counter++;
}

protected int uc_Counter
{
    get { return (int)ViewState["uc_Counter"]; }
    set { ViewState["uc_Counter"] = value; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:51:21+00:00Added an answer on June 5, 2026 at 1:51 am

    I found my answer in this post .net DropDownList gets cleared after postback

    I changed my counter that I was storing in the viewstate to a session variable.
    Then I moved my reloadControls() function from the Page_Load of the *.ASPX to the Page_Init.

    The key was dynamically adding my user control in the Page_Init so it would be a member of the page before the Viewstate was applied to controls on the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm dynamically adding a custom user control to the page as per this post
I have a grid view in web user control and I'm dynamically adding the
I'm having trouble dynamically adding controls inside an update panel with partial postbacks. I've
I'm having an annoying problem when dynamically adding divs to an existing div. I
How would I go about dynamically adding commas as a user is entering numbers?
so I have a problem with dynamically adding items to menuStrip. I mean I
I am dynamically adding inputs to a form when a user clicks on an
I am dynamically adding and replacing controls in a winform panel at runtime Even
I'm currently adding custom label controls dynamically at runtime in my WPF app. I've
So the problem is the following : i do have own user control. which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.