Solved

Dynamically created dropdown does not fire OnSelectedIndexChanged

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in script 11 46
C# Single Form 8 27
I need help making a collection from a class in VB.Net 3 26
Executing SSIS with a Stored Procedure Permissions 12 16
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

23 Experts available now in Live!

Get 1:1 Help Now