user3239820
user3239820

Reputation:

How to bind existing dropdown list defined in .aspx with C#

I have a dropdown list in design page as shown below:

<asp:DropDownList ID="ddlArtList" runat="server">
  <asp:ListItem Value="95">Select</asp:ListItem>
  <asp:ListItem Value="1">1</asp:ListItem>
  <asp:ListItem Value="2">2</asp:ListItem>
  <asp:ListItem Value="3">3</asp:ListItem>
  <asp:ListItem Value="4">4</asp:ListItem>
  <asp:ListItem Value="5">5</asp:ListItem>
  <asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>

This items above are sometimes overridden by some other values in C#, according to the requirement. But at the end i want to bind the above default items with the help of C# to get the above listitems.

I want to know is there any In-Built method or attribute to bind the dropdownlist(.aspx) in C#.

Without using this: ddlArtList.Items.Add ("1); etc etc.

Thanks in advance.

Upvotes: 1

Views: 1547

Answers (3)

Ed Morrison
Ed Morrison

Reputation: 41

If you want to append to a default list, use the markup as you provided to set the default list. You will want to set AppendDataBoundItems to true, as already mentioned.

<asp:DropDownList ID="ddlArtList" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="95">Select</asp:ListItem>
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
    <asp:ListItem Value="4">4</asp:ListItem>
    <asp:ListItem Value="5">5</asp:ListItem>
    <asp:ListItem Value="6">6</asp:ListItem>
 </asp:DropDownList>

In the code-behind, you can set the DataSource to the appended items and simply call DataBind. That will append those items to your dropdownlist.

ddlArtList.DataSource = new List<int>{ 10, 11, 12 }; // replace with actual data source you are using
ddlArtList.DataBind();

Depending on all of your needs, you can add those appended items on the page's Load event or inside another event handler, such as a button click or selecting from another dropdownlist or whatever.

Upvotes: 0

th1rdey3
th1rdey3

Reputation: 4358

You can keep the default list in Session during first Page Load -

if(!isPostback)
{
    ListItem[] items = new ListItem[ddlArtList.Items.Count];
    ddlArtList.Items.CopyTo(items, 0);
    Session["ddlArtList"] = items;
}

Now when you want to reset the list -

if(Session["ddlArtList"] != null)
{
    ListItem[] items = Session["ddlArtList"] as ListItem[];
    ddlArtList.Items.Clear();
    ddlArtList.Items.AddRange(items);
}

Upvotes: 2

Dgan
Dgan

Reputation: 10275

Use AppendDataBoundItems

.aspx Code:

<asp:DropDownList ID="ddlArtList" AppendDataBoundItems="True" runat="server">
  <asp:ListItem Value="95">Select</asp:ListItem>
  <asp:ListItem Value="1">1</asp:ListItem>
  <asp:ListItem Value="2">2</asp:ListItem>
  <asp:ListItem Value="3">3</asp:ListItem>
  <asp:ListItem Value="4">4</asp:ListItem>
  <asp:ListItem Value="5">5</asp:ListItem>
  <asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>

ServerSide:

ddlArtList.AppendDataBoundItems="True"

Upvotes: 2

Related Questions