添加“选择”下拉菜单中的项目

发布于 2024-08-05 19:19:17 字数 189 浏览 7 评论 0原文

我正在使用 ASP.NET 动态数据。在 Insert.aspx 页面中,我有几个下拉列表可供选择。 Dropdown 表示的字段是数据库中的必填字段。因此,下拉列表不会将“选择”显示为下拉列表中的默认选项。我想在下拉列表中显示的数据库中的其他记录顶部添加“选择”选项。请注意,该字段不是必填字段,因此默认情况下动态数据不会显示“选择”选项。我怎样才能做到这一点?

I am using ASP.NET dynamic data. In Insert.aspx page, I have a couple of dropdowns to be selected. The fields represented by Dropdown are Required fields in database. So dropdown does not show 'Select' as default option in dropdown. I want to add 'Select' option at top of other records from database shown in dropdown. Note that field is not Required field, hence 'Select' option is not being show over there by dynamic data by default. How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

宣告ˉ结束 2024-08-12 19:19:17

使用 Insert 方法绑定下拉列表后添加“选择”项:

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

Add your "select" item after binding your dropdownlists by using 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));
铁憨憨 2024-08-12 19:19:17

我将为必需的下拉字段创建一个自定义 FieldTemplate。在控件的 OnDataBinding 事件期间插入“Select”项。我还会有一个客户端RequiredFieldValidator,以确保在回发之前选择“Select”以外的其他内容。

<%@ 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", ""));
   }
  }

-

您必须使用 UIHint 属性,以便使用此 FieldTemplate 代替默认值。

I would create a custom FieldTemplate for Required DropDown fields. Insert the "Select" item during the OnDataBinding event of the control. I would also have a client side RequiredFieldValidator to make sure something other than "Select" is chosen 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 would have to use the UIHint attribute so that this FieldTemplate would be used over the default.

深者入戏 2024-08-12 19:19:17

在索引 0 处添加顶部元素效果很好,但 LINQ 提供了另一种可能性,即返回“选择”或“选择某些内容”或“全部”作为数据的一部分。下拉列表已数据绑定到此函数:

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

Adding a top element at index 0 works just fine, but LINQ offers another possibility, to return the "select" or "pick something" or "all" as part of the data. A drop-down list was databound 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();
    }
}
森罗 2024-08-12 19:19:17
 //---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;
    }
 //---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;
    }
万水千山粽是情ミ 2024-08-12 19:19:17

从哑表中选择前 1 个“0”作为 valueField,“--Select--”作为 textField
联盟
从后端的表格控件中选择 ID、文本

提供了更大的灵活性

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

Control from backend gives much more flexibility

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文