Solved

Dynamically created dropdown does not fire OnSelectedIndexChanged

Posted on 2008-10-21
4
811 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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