Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# ASP.NET服务器将datatable绑定到dropdownlist时出错_C#_Asp.net_Datatable - Fatal编程技术网

C# ASP.NET服务器将datatable绑定到dropdownlist时出错

C# ASP.NET服务器将datatable绑定到dropdownlist时出错,c#,asp.net,datatable,C#,Asp.net,Datatable,下面是我非常简单的测试下拉列表: <asp:DropDownList runat="server" ID="Test" /> 当我尝试加载页面时,出现以下错误: Server Error in '/' Application. DataBinding: 'System.Char' does not contain a property with the name 'Text'. 有人知道这里发生了什么吗?您应该这样引用数据源: Test.DataSource = dt; 要引用

下面是我非常简单的测试下拉列表:

<asp:DropDownList runat="server" ID="Test" />
当我尝试加载页面时,出现以下错误:

Server Error in '/' Application.
DataBinding: 'System.Char' does not contain a property with the name 'Text'.

有人知道这里发生了什么吗?

您应该这样引用数据源:

Test.DataSource = dt;

要引用DataTable对象,请不要使用“dt”字符串。

dt
不是字符串,它是
变量
,并检查
自动回发
,以便
下拉列表
仅绑定一次

    protected void Page_Load(object sender, EventArgs e)
    {

    if(!Page.IsPostBack)

     {

    DataTable dt = new DataTable(); 
    dt.Columns.Add(new DataColumn("Value", typeof(Int32)));
    dt.Columns.Add(new DataColumn("Text", typeof(String)));

    DataRow dr = dt.NewRow(); 
    dr[0] = 1;
    dr[1] = "Test of text";
    dt.Rows.Add(dr); 

    Test.DataSource = dt;
    Test.DataValueField = "Value";
    Test.DataTextField = "Text";
    Test.DataBind();


     }


    }

是的,我问了这个问题不久就明白了,但当我回来回答我自己的问题时,你已经超过了我。谢谢你的快速回复!这只是一个测试页面,所以我没有检查回发。@9988ScooterGirl根据我的经验,良好的习惯会让你做到最好……)
    protected void Page_Load(object sender, EventArgs e)
    {

    if(!Page.IsPostBack)

     {

    DataTable dt = new DataTable(); 
    dt.Columns.Add(new DataColumn("Value", typeof(Int32)));
    dt.Columns.Add(new DataColumn("Text", typeof(String)));

    DataRow dr = dt.NewRow(); 
    dr[0] = 1;
    dr[1] = "Test of text";
    dt.Rows.Add(dr); 

    Test.DataSource = dt;
    Test.DataValueField = "Value";
    Test.DataTextField = "Text";
    Test.DataBind();


     }


    }