Question

I am dynamically creating multiple dropdownlists (with unique ids each) and binding them to a panel. I have also written the SelectedIndexChanged code for them which gets fired everytime a dropdown is changed. My problem is as there is only one SelectedIndexChanged event, I need to get the id of the dropdownlist which is changed. How can I get that?

Here is my code for binding the dropdownlists to the panel:

 foreach (Document offer in parent.Children)
            {
    Panel pnlCat = new Panel();
                    pnlCat.ID = offer.Id.ToString();
                    pnlCat.CssClass = "ngx";
    DropDownList ddl = new DropDownList();
                    ddl.ID = offer.Id + "_cat";
                    ddl.DataTextField = "catName";
                    ddl.DataValueField = "catId";
                    ddl.CssClass = "ddlStyle";
                    ddl.DataSource = category;
                    ddl.DataBind();
                    ddl.AutoPostBack = true;
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
     pnlCat.Controls.Add(ddl);

}

and here is the SelectedIndexChanged code:

void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = this.ClientID;
        }

In the id i get "_page". How can i get the dropdownlist ID?

Was it helpful?

Solution

The sender refers to the object that invoked the event that fired the event handler.

void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    string id = ddl.ID ;
}

OTHER TIPS

string id = (sender as DropDownList).ClientID;

first parameter contains object in which event has been triggered

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top