Solved

Dynamically created dropdown does not fire OnSelectedIndexChanged

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 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
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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