Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DataTextField和DataValueField从另一个列表复制dropdownlist_C#_Asp.net_Drop Down Menu - Fatal编程技术网

C# 使用DataTextField和DataValueField从另一个列表复制dropdownlist

C# 使用DataTextField和DataValueField从另一个列表复制dropdownlist,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,我创建了一个数据层方法 public static List<SegmentBL> GetAllSegment(string SortDirection, string SortExpression) { var ds = DBHelper.GetDatabase().ExecuteDataSet("UDS_Select_SegmentMaster"); var val = ds.Tables[0].AsEnumerable().Sel

我创建了一个数据层方法

  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;
    }
最后介绍了一种填充dropdownlist的表示层方法

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

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

除了DataTextField和DataValueField分配不正确外,它工作正常。当前DataTextField和DataValueField相同。我在上面的代码中犯了什么错误。

在元素添加到数据源之前绑定,在元素添加之后绑定。您可以在
GetAll
方法中创建本地下拉列表,然后将
dropdownlist传递给您的方法

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;
}
移动Databind()行


不确定业务逻辑是否应该创建UI元素-您可以将数据源项返回到表示层吗。尝试绑定同一个东西两次似乎不合逻辑。因为我在应用程序中使用了超过20次的同一行代码。嘿,我需要将ref DropDownList列表作为参数传递给它们。
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;
}
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
    }