Solved

Why is my dynamic bound dropdown losing its value?

Posted on 2008-10-24
3
675 Views
Last Modified: 2010-05-18
I have a form that is created dynamically based on a widget type selected, e.g. Article Widget.  The widget will then tell the code what controls need to be created in the grid, e.g. in this example its 6 dropdown lists, 3 for the categories and 3 for the pages - these are paired together.

Now I can bind it fine, and all the dropdowns appear correctly.  All the category dropdowns have the correct details in, and the page dropdowns each have the first item set to "select a category".  All good so far.

I select the first dropdown and the event that I added to the dynamically bound dropdown correctly fires e.g. ddlCategory.SelectedIndexChanged += new EventHandler(pageCategory_SelectedIndexChanged); and its paired dropdown correctly gets filled with the pages for that category.  All other dropdowns at this point are still in their initial state.  So all controls are being rebuilt on postback, and the viewstate for the category dropdown is being remebered.  Again all good so far.

Now when I select a category in one of the other category dropdowns the SelectedIndexChanged event fires for both the first category dropdown and then the second dropdown.  This again I believe is correct behaviour, so that the page dropdowns can then have the correct items in them to allow the viewstate to be put back correctly.  However the first page dropdownlist loses its selected value - as if the viewstate is not being populated back.

Has anyone come across this before?
0
Comment
Question by:ezekial316
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 

Author Comment

by:ezekial316
ID: 22794363
This controls are all being bound in a repeater so I thought I should add the repeater bound event.
protected void rptControls_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CMS_ControlMeta meta = (CMS_ControlMeta)e.Item.DataItem;
 
            switch (meta.InputType)
            {                
                case WidgetPropertyInformation.InputTypes.PageSelector: //page selector
 
                    DropDownList ddlFirst = new DropDownList();
                    DropDownList ddlSecond = new DropDownList();
 
                    CMS_CategoryService service = new CMS_CategoryService();
                    
                    ddlFirst.CssClass = "control";                    
                    ddlFirst.AutoPostBack = true;
                    
                    ddlFirst.SelectedIndexChanged += new EventHandler(pageCategory_SelectedIndexChanged);
                    
                    ddlFirst.ID = "ddlFirst" + e.Item.ItemIndex.ToString();
                    ddlFirst.DataSource = service.FetchCategoryTree();
                    ddlFirst.DataTextField = "Title";
                    ddlFirst.DataValueField = "ID";
                    ddlFirst.DataBind();
                    ddlFirst.Items.Insert(0, new ListItem("select", "select"));
 
                    (e.Item.FindControl("litLabel") as Literal).Text = meta.Name;
 
                    e.Item.FindControl("pair").Visible = true;
 
                    ddlSecond.ID = "ddlSecond" + e.Item.ItemIndex.ToString();
                    ddlSecond.CssClass = "control";                    
 
                    (e.Item.FindControl("litPair") as Literal).Text = "Select Page";
                    (e.Item.FindControl("phControls") as PlaceHolder).Controls.Add(ddlFirst);
                    (e.Item.FindControl("phControls2") as PlaceHolder).Controls.Add(ddlSecond);
 
                    break;                
            }
 
 
        }
    }

Open in new window

0
 

Author Comment

by:ezekial316
ID: 22794367
And here is the code for the Item Selected Event
protected void pageCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            RepeaterItem item = ddl.Parent.Parent as RepeaterItem;
            DropDownList ddl2 = (DropDownList)item.FindControl("ddlSecond" + item.ItemIndex.ToString());
 
            string id = ddl.ID;
 
            CMS_PageService service = new CMS_PageService();
            List<CMS_Page> pages = service.FetchAllByCategoryID(Convert.ToInt32(ddl.SelectedValue), false);
 
            ddl2.DataSource = pages;
            ddl2.DataTextField = "Title";
            ddl2.DataValueField = "ID";
            ddl2.DataBind();
 
            ddl2.Items.Insert(0, new ListItem("select", "select"));                        
        }
    }

Open in new window

0
 

Accepted Solution

by:
ezekial316 earned 0 total points
ID: 22795539
Solved.

All down to .net life cycle and using ItemCreated instead of ItemDataBound.

Done!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question