Solved

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

Posted on 2009-05-15
4
760 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
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 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

22 Experts available now in Live!

Get 1:1 Help Now