尝试从数据集填充网站(webform)文本框

时间:2012-02-02 20:11:52

标签: asp.net c#-4.0 webforms

我无法将DataSet转换为名为PrefixDescription的webform文本框。我试图将行转换为字符串,然后我尝试将字符串放入文本框中。但是,文本框中没有任何内容。 DataSet确实有数据。我尝试过数据绑定和数据绑定,但这些都不起作用。

 private DirectoryEntry testAD = new DirectoryEntry();
 private DataTable DT = new DataTable();

 protected void Button2_Click(object sender, EventArgs e)
 {
 DirectorySearcher search = new DirectorySearcher(testAD);
        SearchResultCollection myResults = search.FindAll();
        search.PropertiesToLoad.Add("name");
        DT.Columns.Add("name");
        DT.Columns.Add();

        foreach (SearchResult SR in myResults)
        {
            DataRow dr = DT.NewRow();
            DirectoryEntry DE = SR.GetDirectoryEntry();
            dr["name"] = DE.Properties["name"].Value;
            DT.Rows.Add(dr);
            DT.AcceptChanges();
            PrefixDescription.Text = Convert.ToString(dr["name"]);
            DE.Close();
        }
}

1 个答案:

答案 0 :(得分:1)

更好的是,使用StringBuilder,类似这样......

    System.Text.StringBuilder builder = new System.Text.StringBuilder();

    foreach (SearchResult SR in myResults)
    {
        DataRow dr = DT.NewRow();
        DirectoryEntry DE = SR.GetDirectoryEntry();
        dr["name"] = DE.Properties["name"].Value;
        DT.Rows.Add(dr);
        DT.AcceptChanges();
        builder.Append(Convert.ToString(dr["name"]));
        PrefixDescription.Text = Convert.ToString(dr["name"]);
        DE.Close();
    }

    PrefixDescription.Text = builder.ToString();