Add Select Item to DropDown

I am using ASP.NET dynamic data. On the Insert.aspx page, I have several dropdowns. The fields provided by Dropdown are required in the database. So the dropdown does not show "Select" by default in the dropdown. I want to add a "Select" option on top of other records from the database shown in the dropdown. Note that the field is optional, so the Select option is not displayed with dynamic data by default. How can i do this?

+2


source to share


5 answers


Add your "select" element after binding the dropdownlists using the Insert method:



myDropDownList.DataBind();
// To make it the first element at the list, use 0 index : 
myDropDownList.Items.Insert(0, new ListItem("Select", string.Empty));

      

+8


source


I would create custom FieldTemplate for the required DropDown fields. Insert a Select element during the control's OnDataBinding event. I would also have a RequiredFieldValidator client to make sure something other than Select is selected before it can post back.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ForeignKeyRequired_Edit.ascx.cs"
 Inherits="DDWANorthwind.DynamicData.FieldTemplates.ForeignKeyRequired_Edit" %>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="droplist">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
 ControlToValidate="DropDownList1" ErrorMessage="Selection Required"></asp:RequiredFieldValidator>

      

-



  protected override void OnDataBinding(EventArgs e)
  {
   base.OnDataBinding(e);

   if (Mode == DataBoundControlMode.Edit)
   {
    string foreignkey = ForeignKeyColumn.GetForeignKeyString(Row);
    ListItem item = DropDownList1.Items.FindByValue(foreignkey);
    if (item != null)
    {
     DropDownList1.SelectedValue = foreignkey;
    }
   }
   else if (Mode == DataBoundControlMode.Insert &&
    Column.IsRequired)
   {
    DropDownList1.Items.Insert(0, new ListItem("Select", ""));
   }
  }

      

-



you will need to use the UIHint attribute for this FieldTemplate to be used by default.

+1


source


Adding the top item to index 0 works just fine, but LINQ offers another option to return "select" or "select something" or "all" as part of the data. The dropdown has been tied to this function:

public static List<string> GetRegions()
{
    using (NorthwindDataContext nw = new NorthwindDataContext())
    {
        IQueryable<string> regionQuery = nw.Customers
            .Where(c => c.Region != null)
            .OrderBy(c => c.Region)
            .Select(c => c.Region)
            .Distinct();
        return (new List<string>() { "All" }).Concat(regionQuery).ToList();
    }
}

      

+1


source


 //---Populate Category DropDownList
    private void getData()
    {

        var category = (from c in CoffeeContext.tblProductTypes
                        select new { c.ProductType, c.Description }).ToList();
        cbxCategory.DataTextField = "Description";
        cbxCategory.DataValueField = "ProductType";
        cbxCategory.DataSource = category;
        cbxCategory.DataBind();
        cbxCategory.Items.Insert(0, "--Select Type--");
        cbxCategory.SelectedIndex = 0;
    }

      

0


source


Select Top 1 '0' as valueField, '--Select--' as textField from dummtable UNION select ID, text from table TABLE

Backend management gives you much more flexibility

0


source







All Articles