Showing posts with label DropDownList. Show all posts
Showing posts with label DropDownList. Show all posts

Monday, January 27, 2014

Using a Dictionary as the DataSource for a DropDownList

It is possible to use a Dictionary or even Dictionary<string> as the DataSource for a DropDownList directly, without having to iterate through the the Dictionary's KeyValuePair collection and populate the DropDownList's Items collection manually.

The text member is "value" and the value member is "key" when using one of these types of objects to bind to a DropdownList.

Dictionary<byte> dicTable = new Dictionary<byte>();
dicTable.Add(1, "One");
dicTable.Add(2, "Two");
dicTable.Add(3, "Three");
dicTable.Add(4, "Four");
cmbDropDownList.DataSource = dicTable;
cmbDropDownList.DataTextField = "Value";
cmbDropDownList.DataValueField = "Key";
cmbDropDownList.DataBind();

Submit this story to DotNetKicks

Wednesday, December 21, 2011

Using required field validator with dropdownlist

The requiredfieldvalidator can be used for a dropdown list, you just need to set InitialValue property of the RequiredFieldValidator with the InitialValue of the DropDownList to make it work

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>--Select--</asp:ListItem>
    <asp:ListItem>First Item</asp:ListItem>
    <asp:ListItem>Second Item</asp:ListItem>
    <asp:ListItem>Third Item</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
      ControlToValidate="DropDownList1"
      ErrorMessage="Value Required!"
      InitialValue="--Select--">
</asp:RequiredFieldValidator>


Submit this story to DotNetKicks