Thursday, April 22, 2010

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




Please visit my new Web Site https://coderstechzone.com



In most of the cases developers need to add dynamic controls in asp.net 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 asp.net 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.

If you run the below example code and select any item from dynamically created DropdownList then you will get the below interface:
Dynamic DropdownList

To add a asp.net server side DropdownList control when page is loaded, we need to write the below code segment under page_Load Event:
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);

        this.form1.Controls.Add(ComboBox);
    }

Then add the below handler function:
private void Dynamic_Method(object sender, EventArgs e)
    {
        //Response.Write(((DropDownList)sender).SelectedIndex.ToString());
        //Response.Write(Request.Form["ComboBox"]);
        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.Text = sSTR;
        this.form1.Controls.Add(lbl);
    }

Note: If you want to read DropdownList selectedvalue from other control postback like button control then you can read the above dynamically created DropdownList selectedvalue in the following way:
Response.Write(Request.Form["ComboBox"]);

VB.Net Code:
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.Text = sSTR
        Me.form1.Controls.Add(lbl)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ComboBox As DropDownList = 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

        Me.form1.Controls.Add(ComboBox)
    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.

3 comments:

Unknown said...

thank you so much

Anonymous said...

Nice work, and thanks for sharing

Anonymous said...

thnx..

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash