简体   繁体   English

使用DataTextField和DataValueField从另一个列表复制下拉列表

[英]Copy dropdownlist from another list with DataTextField and DataValueField

I create one datalayer method 我创建了一个数据层方法

  public static List<SegmentBL> GetAllSegment(string SortDirection, string SortExpression)
    {

        var ds = DBHelper.GetDatabase().ExecuteDataSet("UDS_Select_SegmentMaster");

        var val = ds.Tables[0].AsEnumerable().Select(r => new SegmentBL
        {
            _SegmentId = Convert.ToInt32(r[0].ToString()),
            _SegmentName = r[1].ToString()
        });
        List<SegmentBL> list = val.ToList();
        return list;
    }

from that I create one Bussiness logic method 从那里我创建了一个Bussiness逻辑方法

public DropDownList GetAll(string SortDirection, string SortExpression)
    {
        var list = new DropDownList();
        list.DataSource = SegmentDL.GetAllSegment(SortDirection, SortExpression);
        list.DataTextField = "_SegmentName";
        list.DataValueField = "_SegmentID";
        list.DataBind();
        ListItem item = new ListItem();
        item.Text = "--Select--";
        item.Value = "0";
        list.Items.Insert(0, item);
        return list;
    }

Finally Presentation Layer Method for filling dropdownlist 最后用于填充下拉列表的表示层方法

 private void FillSegment()
    {
        ddlSegment.DataSource = seg.GetAll(General.SortAscending,"SegmentID").Items;

        ddlSegment.DataBind();
        ddlSegment.DataTextField = "_SegmentName";
        ddlSegment.DataValueField = "_SegmentID";
    }

It's working fine except the DataTextField and DataValueField not assign properly. 除了没有正确分配DataTextField和DataValueField之外,它工作正常。 Currently DataTextField and DataValueField same. 目前DataTextField和DataValueField相同。 What is mistake I did in above code. 我在上面的代码中做了什么错误。

You are binding before the elements are added to the datasource bind after the elements being added. 在添加元素之后将元素添加到数据源绑定之前绑定。 You may pass dropdownlist to your method intead of creating local drop down in GetAll method. 您可以pass dropdownlist pass给在GetAll方法中创建本地下拉列表的方法。

public DropDownList GetAll(string SortDirection, string SortExpression, DropDownList list)
{
  //  var list = new DropDownList(); //Remove this line
    list.DataSource = SegmentDL.GetAllSegment(SortDirection, SortExpression);
    list.DataTextField = "_SegmentName";
    list.DataValueField = "_SegmentID";      
    ListItem item = new ListItem();
    item.Text = "--Select--";
    item.Value = "0";
    list.Items.Insert(0, item);
    list.DataBind();
    return list;
}

Move the Databind() line. 移动Databind()行。

private void FillSegment()
    {
        ddlSegment.DataSource = seg.GetAll(General.SortAscending,"SegmentID").Items;


        ddlSegment.DataTextField = "_SegmentName";
        ddlSegment.DataValueField = "_SegmentID";

        ddlSegment.DataBind(); //After and not before defining the fields value
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DropDownList的DataTextField和DataValueField属性 - DataTextField and DataValueField properties of DropDownList DropDownList:DataTextField,DataValueField ... DataEnabledField? - DropDownList: DataTextField, DataValueField… DataEnabledField? 使用 linq 查询将值存储在下拉列表的 DataValueField 和 DataTextField 中 - Storing values in the DataValueField and DataTextField of a dropdownlist using a linq query 如何从下拉列表中获取DataValueField? - How to get DataValueField from dropdownlist? CheckboxList忽略DataValueField和DataTextField - CheckboxList ignores DataValueField and DataTextField dropdownlist DataTextField由属性组成? - dropdownlist DataTextField composed from properties? Asp.net WebForms DropDownList-具有不同值和显示文本的控件(DataValueField / DataTextField) - Asp.net WebForms DropDownList - controls with different values and displayed texts (DataValueField/DataTextField) 从下拉列表中的datatextfield中的数据表中合并2列 - Merge 2 columns from datatable in datatextfield from dropdownlist 从SQL(C#)加载到ComboBox时无法设置Datatextfield和dataValuefield - Unable To Set Datatextfield and dataValuefield When Loading To ComboBox From SQL (C#) 如何在一个ComboBox中使用两个DataTextField和DataValueField - How to use two DataTextField and DataValueField in one ComboBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM