Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to statically bind a dropdownlist with multiple items
Posted

Hi,

If in design side you want to add static value then,

XML
<asp:DropDownList ID="ddl" runat="server">
            <asp:ListItem Text="test1" Value="test1"></asp:ListItem>
            <asp:ListItem Text="test2" Value="test2"></asp:ListItem>
            <asp:ListItem Text="test3" Value="test3"></asp:ListItem>
        </asp:DropDownList>



If on coding side you want to add static value then,

C#
ddl.Items.Insert(0, new ListItem("test1", "test1"));
 ddl.Items.Insert(1, new ListItem("test2", "test2"));
 ddl.Items.Insert(2, new ListItem("test3", "test3"));
 
Share this answer
 
Comments
Adarsh chauhan 1-Aug-13 8:43am    
Good answer.. +5
Harshil_Raval 1-Aug-13 8:45am    
Thanks. If you found your answer then mark it as answer. :-)
prthghosh999 2-Aug-13 2:51am    
i want to know the process in mvc
Hello ,

For MVC
You can add one List<selectlistitem> property to your model, And assign the hard coded values to the list.
E.g.

C#
List<SelectListItem> Countries= new List<SelectListItem>();

            result.Add(new SelectListItem() { Text = "India", Value = "India" });
            result.Add(new SelectListItem() { Text = "ShriLanka", Value="ShriLanka" });
            result.Add(new SelectListItem() { Text = "Nepal", Value = "Nepal" });


and assign this list to your dropdown.
 
Share this answer
 
v2
Comments
prthghosh999 2-Aug-13 2:54am    
thanks for giving me the solution.
but if i want to bind numbers from 1 to 30 then what will i do in mvc???
XML
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value="All">All</asp:ListItem>
    <asp:ListItem Value="Valid">Valid</asp:ListItem>
    <asp:ListItem Value="Expired">Expired</asp:ListItem>
</asp:DropDownList>



or by coding

SQL
DropDownList1.Items.Insert(0, new ListItem("All", "All"));
DropDownList1.Items.Insert(1, new ListItem("Valid", "Valid"));
DropDownList1.Items.Insert(2, new ListItem("Expired", "Expired"));
 
Share this answer
 
v2
one way is to put all data in dataset/datatable and bind the data to dropdownlist like ddl.DataSource=dataset
ddl.DataBind()

or u can user ddl.Items.Add method
 
Share this answer
 
If it is MVC, follow below things

In view, add below statement

C#
@Html.DropDownListFor(q => q.Filter.Status, Model.ListOfStatuses)


In service layer add below method
C#
public IEnumerable<system.web.mvc.selectlistitem> GetStatuses()
{
	yield return new System.Web.Mvc.SelectListItem() { Value = "", Text = "All", Selected = true };
	yield return new System.Web.Mvc.SelectListItem() { Value = "A", Text = "Active", Selected = false };
	yield return new System.Web.Mvc.SelectListItem() { Value = "I", Text = "Inactive", Selected = false };
}</system.web.mvc.selectlistitem>


and call this

C#
model.ListOfStatuses = GetStatuses();


Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900