Hi Andrew,
To make ASP.NET fire the ddlGroups_SelectedIndexChanged event, you have to
make sure the dynamically created DropDownList exist in the control
hierarchy when the page is postback. Since the DropDownList is only
created
in the first DropDownList's SelectedIndexChanged event, it's not
re-created
upon the second postback caused by other user action.
To fix it, you can use several options.
1) Use a viewstate variable to flag whether we need the second
DropDownList
or not, then re-create it in Page_Load if needed. This will make sure the
event get's fired correctly.
protected void Page_Load(object sender, EventArgs e)
{
if (NeedDropDownList)
{
CreateDropDownList(true);
}
}
protected void CreateDropDownList(bool flag)
{
const string THE_ID = "ddl2";
if (flag)
{
DropDownList ddl2 = new DropDownList();
ddl2.SelectedIndexChanged += new
EventHandler(ddl2_SelectedIndexChanged);
ddl2.ID = THE_ID;
ddl2.AutoPostBack = true;
ddl2.Items.Add(new ListItem("1", "value1"));
ddl2.Items.Add(new ListItem("2", "value2"));
form1.Controls.Add(ddl2);
}
else
{
form1.Controls.Remove(form1.FindControl(THE_ID));
}
NeedDropDownList = flag;
}
void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl2 = sender as DropDownList;
Response.Write(ddl2.SelectedValue);
CreateDropDownList(false);
}
protected bool NeedDropDownList
{
get
{
object o = ViewState["A"];
if (o == null) return false;
return (bool)o;
}
set { ViewState["A"] = value; }
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
CreateDropDownList(true);
}
2) Since in this case the DropDownList's SelectedIndexChanged event is
simple, all you wanted is the selected item's text or value, therefore I
think you don't need the event at all, just check the Request.Form
collection to see if it's there.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
object o = Request.Form["ddl2"];
if (o != null)
{
Response.Write(o);
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
DropDownList ddl2 = new DropDownList();
ddl2.ID = "ddl2";
ddl2.AutoPostBack = true;
ddl2.Items.Add(new ListItem("1", "value1"));
ddl2.Items.Add(new ListItem("2", "value2"));
form1.Controls.Add(ddl2);
}
Please note this second approach do have one difference with the first
approach: if the postback is caused by other controls (for example: a
button's click) after the second dropdownlist is created dynamically, in
Page_Load you will still find the ddl2's value in Request.Form regardless
if the selected index is changed or not. In the first approach, the
SelectedIndexChanged event of the second dropdownlist only fires when the
index is really changed.
Hope this helps.
Sincerely,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.