1

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.

3 Answers 3

2

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Values are not overriding the other. infact all the values are getting bind to the dropdownlist.
@Pranav can you show in your code how you are binding or adding new items to it
ClientSide: would be aspx or html side. As the <asp:DropDownList ID="ddlArtList" AppendDataBoundItems="True" ... is server side code
2

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);
}

Comments

0

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.

1 Comment

No I did this in Visual Studio and it works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.