?
Solved

How to assign SelectedIndex Change Event of a Drop Down List c#

Posted on 2009-05-15
4
Medium Priority
?
771 Views
Last Modified: 2012-05-07
Hello Experts,

I need to assign a method to the 'SelectedIndexChanged' event of a drop down list programmatically.  How do I accomplish this?  It's simple enough to assign this property from an aspx page, but I am creating the control in code and need to assign the method in the code behind.

Any help is appreciated.

ddlList

MethodToFireOnSelectedIndexChange()
0
Comment
Question by:soapygus
[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 9

Expert Comment

by:VikramMullick
ID: 24396514
In the aspx page for ddllist attach the event , OnSelectedIndexChanged="MethodToFireOnSelectedIndexChange"
Remember to set the property autopostback=true for the dropdown.
In the aspx.cs page write
 protected void MethodToFireOnSelectedIndexChange(object sender, EventArgs e)
    {

}
0
 
LVL 16

Accepted Solution

by:
CuteBug earned 2000 total points
ID: 24396546

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.MethodToFireOnSelectedIndexChange);
 
private void MethodToFireOnSelectedIndexChange(object sender, EventArgs e)
{
   // Add your code here
}

Open in new window

0
 
LVL 3

Expert Comment

by:channa_m
ID: 24397052
Before adding the control you need add the event handler, follow the code below
   protected void Page_Load(object sender, EventArgs e)
    {
        // Create dropdown List
        DropDownList ddlYear = new DropDownList();
        ddlYear.ID = "ddlYear";
 
        // Add Items
        ListItem li = new ListItem("2008", "2008");
        ddlYear.Items.Add(li);
        li = new ListItem("2009", "2009");
        ddlYear.Items.Add(li);
 
        ddlYear.AutoPostBack = true;
 
        // Add event handler
        ddlYear.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
 
        // Add the control to the form
        this.FindControl("form1").Controls.Add(ddlYear);
    }
 
    void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlSelected = (DropDownList)sender;
        tb1.Text = ddlSelected.SelectedItem.Value;
    }
 
 
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
    </form>
</body>

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 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