Kevin M
Kevin M

Reputation: 5506

Dynamically loaded dropdownlist control doesn't fire SelectedIndexChanged event

I have the control dropdownlist which has been loaded inside the template column of RadGrid.
While loading I have set AutoPostBack='True' for dropdownlist and also created the event SelectedIndexChanged.

 DropDownList ddlConditions = new DropDownList(); 
 ddlConditions.ID = "ddl" + name;
 ddlConditions.AutoPostBack = true;
 ddlConditions.SelectedIndexChanged += new EventHandler(ddlConditions_SelectedIndexChanged);

My question is while i change the selected index of dropdownlist the event SelectedIndexChanged is not getting triggered.
Can anyone help me to solve this problem?
Thanks in advance.

Upvotes: 1

Views: 2769

Answers (3)

KoViMa
KoViMa

Reputation: 382

I can suggest you to check the place where you have created DropDownList. Dynamic controls should be added on OnInit or at least on OnLoad. After OnLoad finishes executing ASP.NET starts processing control's events and values.

My question is while i change the selected index of dropdownlist the event SelectedIndexChanged is not getting triggered.

Answer: because you have created DropDownList after the events have been processed.

Upvotes: 1

Bhavik Patel
Bhavik Patel

Reputation: 772

Usually this caused by a page life cycle problem. When your index changed event of Dropdownlist fires the control doesn't exist to bind it on the postback.

Example:

-> MyEvent fires. -> Drop-down created. -> Event Handler specified. -> Index Changed event triggered. Page reloads. Drop-down not found, cannot fire.

So you have to ensure that the drop-down is created before .NET attempts to handle the event.

Please refer this answer for more information regarding this type of issue and life cycle.

Upvotes: 1

RemarkLima
RemarkLima

Reputation: 12037

Is the page posting back? If so, you'll need to make sure that the control is recreated on the page on every postback.

If it's inside the usual if(!IsPostBack) block, then put it outside - Usually, it's prudent to create controls in page_init as well, but that can depend on your specific setup.

Upvotes: 0

Related Questions