DropDownList Programmatically add items on Button Click in ASP.Net using C# Example

Programmatically add items:



DropDownList Programmatically example: 

how to add programmatically ListItem in DropDownList.Asp.net  this dropdownlist web control enables user to choose  a particular item from given item list. Dropdownlist  display to collection of items as a List in asp.net. 

There are two  properties  in DropDownlist item in asp.net:

First is “Text” properties and second is “Value” properties.
  • Text property : Gives the content which display on web browser.
  • Value property: Refres a hidden field text related to specific list item.

We can populate dropdownlist from various data in List. And we also can add item inlist of dropdownlist control Programmatically.

 In this post we consider following example code demonstrates that how can we add listitem in dropdownlist control with myddl id by programmatically (dynamically using c# or csharp code). Here we create a script section on web page head section.

<head id="Head1" runat="server">
    <title> Programmatically add items to DropDownList example: how to add ListItem Programmatically </title>
    <script runat="server"> 
        protected void Btn_Click(object sender, System.EventArgs e)
        {
            Myddl.Items.Add(new ListItem(Txtbox.Text));
            Response.Write("Item Added: " + Txtbox.Text);
        } 
    </script>
</head>

ProgrammaticallyAddListItem_in_DropDownList.aspx

Code Example of programmatically DropDowmlist”:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title> Programmatically add items to DropDownList example: how to add ListItem Programmatically </title>
    <script runat="server"> 
        protected void Btn_Click(object sender, System.EventArgs e)
        {
            Myddl.Items.Add(new ListItem(Txtbox.Text));
            Response.Write("Item Added: " + Txtbox.Text);
        } 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        Example of DropDownlist Insert item Programmatically in asp.net:<br />
        <asp:DropDownList ID="Myddl" runat="server">
            <asp:ListItem>Listitem_first</asp:ListItem>
            <asp:ListItem> Listitem_second</asp:ListItem>
            <asp:ListItem> Listitem_thred</asp:ListItem>
            <asp:ListItem> Listitem3_Fourth</asp:ListItem>
            <asp:ListItem>List item4_five</asp:ListItem>
            <asp:ListItem> Listitem5_six</asp:ListItem>
            <asp:ListItem> item_seven/asp:ListItem>
        </asp:DropDownList>
        <hr />
        <asp:TextBox ID="TxtBox" runat="server"></asp:TextBox>
        <hr />
        <asp:Button ID="Btn" runat="server" Text="Add Item" OnClick="Btn_Click" />
        <hr />
    </div>
    </form>
</body>

</html>


Comments

Popular posts from this blog