Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 数据绑定:';System.Web.UI.Pair';不包含名为';第一';_C#_Asp.net_.net_Vb.net_Drop Down Menu - Fatal编程技术网

C# 数据绑定:';System.Web.UI.Pair';不包含名为';第一';

C# 数据绑定:';System.Web.UI.Pair';不包含名为';第一';,c#,asp.net,.net,vb.net,drop-down-menu,C#,Asp.net,.net,Vb.net,Drop Down Menu,我正在将数据绑定到一个下拉列表,由于某种原因,它不起作用,我很感兴趣 我使用的代码是: public void BindDropDown(List<Pair> dataList) { ddlGraphType.DataTextField = "First"; ddlGraphType.DataValueField = "Second"; ddlGraphType.DataSource = dataList; ddlGraphType.DataBind

我正在将数据绑定到一个下拉列表,由于某种原因,它不起作用,我很感兴趣

我使用的代码是:

public void BindDropDown(List<Pair> dataList)
{
    ddlGraphType.DataTextField = "First";
    ddlGraphType.DataValueField = "Second";

    ddlGraphType.DataSource = dataList;
    ddlGraphType.DataBind();
}
提前谢谢

已添加


我知道异常意味着什么,但pair对象确实包含第一个和第二个属性,这就是问题所在。

First
Second
字段不是
pair
类型的属性。您需要创建一个具有两个属性的类:

 public class NewPair
 {
    public string First { get; set; }
    public string Second { get; set; }
 }
编辑:使用:由@Damien_The_unsiver和@Chris Chilvers建议

List<Tuple<string, string>> list = new List<Tuple<string, string>>()
 {
   new Tuple<string,string>("One","1"),
   new Tuple<string,string>("Two","2"),
};

ddlGraphType.DataTextField = "Item1";
ddlGraphType.DataValueField = "Item2";

ddlGraphType.DataSource = list;
ddlGraphType.DataBind();
List List=新列表()
{
新元组(“一”、“1”),
新元组(“2”,“2”),
};
ddlGraphType.DataTextField=“Item1”;
ddlGraphType.DataValueField=“Item2”;
ddlGraphType.DataSource=列表;
ddlGraphType.DataBind();

第一个
第二个
字段不是
类型的属性。您需要创建一个具有两个属性的类:

 public class NewPair
 {
    public string First { get; set; }
    public string Second { get; set; }
 }
编辑:使用:由@Damien_The_unsiver和@Chris Chilvers建议

List<Tuple<string, string>> list = new List<Tuple<string, string>>()
 {
   new Tuple<string,string>("One","1"),
   new Tuple<string,string>("Two","2"),
};

ddlGraphType.DataTextField = "Item1";
ddlGraphType.DataValueField = "Item2";

ddlGraphType.DataSource = list;
ddlGraphType.DataBind();
List List=新列表()
{
新元组(“一”、“1”),
新元组(“2”,“2”),
};
ddlGraphType.DataTextField=“Item1”;
ddlGraphType.DataValueField=“Item2”;
ddlGraphType.DataSource=列表;
ddlGraphType.DataBind();

t表示目标属性必须是依赖项属性。这也意味着您不能绑定字段和对。首先是字段不是属性

这意味着目标属性必须是依赖属性。这也意味着不能绑定字段和对。首先是字段不属性

public sealed class Pair
{
}
字段:

Public field    First   Gets or sets the first object of the object pair.
Public field    Second  Gets or sets the second object of the object pair.

字段:

Public field    First   Gets or sets the first object of the object pair.
Public field    Second  Gets or sets the second object of the object pair.

请参阅。

在声明属性之后,您可能忘记了
{get;set;}

public class A
{

  //This is not a property
  public string Str;

 //This is a property
  public string Str2 {get; set;}

}

在声明属性之后,您可能忘记了
{get;set;}

public class A
{

  //This is not a property
  public string Str;

 //This is a property
  public string Str2 {get; set;}

}

或者,在.net 4中,您可以使用TupleFor.net 4或更高版本,
Tuple
可能是一个合适的替代品-它确实实现了属性而不是字段。@Damien_The_unsiever&Chris Chilvers感谢buddy!我很感激你的建议。或者,在.net 4中,你可以使用TupleFor.net 4或更高版本,
Tuple
可能是一个合适的替代品-它确实实现了属性而不是字段。@Damien_The_unsiver&Chris Chilvers谢谢你,伙计!我感谢你的建议。