Runtime Add Dynamic DropdownList And Handle SelectedIndexChanged Event in ASP.NET C# VB.NET

In most of the cases developers need to add dynamic controls in application using code behind or runtime. Here in this article I will explain how one can add dynamically Asp.Net Server side DropdownList control in runtime and assign or handle SelectedIndexChanged event in using C# and VB.NET. Most of the developers claims that dynamic controls loose data after postback. But if you run this example program you will not face the problem. Don’t wrap up the codes under page_laod event within not ispostback condition.

Runtime Add Dynamic DropdownList And Handle SelectedIndexChanged Event in ASP.NET Csharp VB.Net

Create Runtime DropdownList Control and its SelectedIndexChanged Handler:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
        DropDownList ComboBox = new DropDownList();
        ComboBox.ID = "ComboBox";
        ComboBox.AutoPostBack = true;

        ComboBox.Items.Add(new ListItem("Year: 2010", "2010"));
        ComboBox.Items.Add(new ListItem("Year: 2011", "2011"));
        ComboBox.Items.Add(new ListItem("Year: 2012", "2012"));
        ComboBox.Items.Add(new ListItem("Year: 2013", "2013"));
        ComboBox.Items.Add(new ListItem("Year: 2014", "2014"));

        ComboBox.SelectedIndexChanged += new EventHandler(Dynamic_Method);

    private void Dynamic_Method(object sender, EventArgs e)
        DropDownList ComboBox = (DropDownList)sender;
        string sSTR = "";
        sSTR = "</br>Selected Index: " + ComboBox.SelectedIndex.ToString();
        sSTR += "</br>Selected Item: " + ComboBox.SelectedItem.Text.ToString();
        sSTR += "</br>Selected Value: " + ComboBox.SelectedItem.Value.ToString();

        Label lbl = new Label();
        lbl.Font.Bold = true;
        lbl.Text = sSTR;

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim ComboBox As New DropDownList()
        ComboBox.ID = "ComboBox"
        ComboBox.AutoPostBack = True

        ComboBox.Items.Add(New ListItem("Year: 2010", "2010"))
        ComboBox.Items.Add(New ListItem("Year: 2011", "2011"))
        ComboBox.Items.Add(New ListItem("Year: 2012", "2012"))
        ComboBox.Items.Add(New ListItem("Year: 2013", "2013"))
        ComboBox.Items.Add(New ListItem("Year: 2014", "2014"))

        AddHandler ComboBox.SelectedIndexChanged, AddressOf Dynamic_Method

    End Sub

    Private Sub Dynamic_Method(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ComboBox As DropDownList = CType(sender, DropDownList)
        Dim sSTR As String = ""
        sSTR = "</br>Selected Index: " + ComboBox.SelectedIndex.ToString()
        sSTR += "</br>Selected Item: " + ComboBox.SelectedItem.Text.ToString()
        sSTR += "</br>Selected Value: " + ComboBox.SelectedItem.Value.ToString()

        Dim lbl As Label = New Label()
        lbl.Font.Bold = True
        lbl.Text = sSTR
    End Sub

Hope now you can add runtime dynamically DropdownList control and also handle its all type of event using above code sample or example.

Download Code Example C#        Download Code Example VB.Net

One thought on “Runtime Add Dynamic DropdownList And Handle SelectedIndexChanged Event in ASP.NET C# VB.NET

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code