Solved

Dynamically created dropdown does not fire OnSelectedIndexChanged

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now