Solved

Dynamically created dropdown does not fire OnSelectedIndexChanged

Posted on 2008-10-21
4
810 Views
Last Modified: 2012-06-21
I am dynamically creating a dropdown list and populating the list with xml values. The dropdown list gets display on the page (in a datagrid) correctly. However when the selection is changed. The SelectedIndexChanged event is not getting fired.

The code is as follows :

public class  myform
{
  private CustomDropdownList ddlField = null;
        private const string dropdownXML = "<items type=\"stypes\"><item text=\"My Databases\" value=\"d\"/><item text=\"Other Databases\" value=\"o\"/><item text=\"All\" value=\"a\"/></items>";

protected void Page_Load(object sender, System.EventArgs e)
            {
            
                  if (!IsPostBack)
                  {
                            Buildropdownlist(Currval);
                                                          CreateGrid();
                  }

            }

private void CreateGrid()
            {
            dataGrid.AddControlToPanel("Dropdown name ", ddlfield, HorizontalAlign.Left);
            this.ddlfield.SelectedIndexChanged += new EventHandler(this.OnddlFieldvalChanged);
            dataGrid.ShowGrid(Getdata..),

            }

 private void Buildropdownlist(Currval)
        {
            ddlField = new CustomDropDownList();
            ddlField.ID = "fieldname";

            ddlField.InitTemplatedControl(dropdownXML,theCurrval.ToString());
            ddlField.AutoPostBack = true;
            ddlField.EnableViewState = true;
            ddlField.ChildOfGridTitlePanel = true;

        }
}

Please help.
0
Comment
Question by:Kiran_M
4 Comments
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22772570
Check that you have the property "AutoPostBack" of the DropDown List set to True.
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 22773250
In addition to having AutoPostBack set to true. Try adding the eventHandler to ddlField before adding it to the panel.
0
 
LVL 6

Accepted Solution

by:
AmarIs26 earned 250 total points
ID: 22774668
You need to recreate dynamic controls each time, so you cant use !PostBack.
Just make sure you recreate them everytime and as long as the ID property is set the same way each time, the controls will pickup their viewstate values.
Also make sure you recreate controls in OnInit event because at this stage the viewstate is not loaded. If you recreate them here then by the time OnPageLoad is executed, the controls are there and their viewstate will be loaded.
0
 

Author Closing Comment

by:Kiran_M
ID: 31508560
Thanks Amarls26! Your suggestion worked great.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

778 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