3

ASP.NET 4.0 を使用して C# で Web アプリケーションを作成しています。要件は、組織の階層情報を表示する必要があることです。たとえば、上級管理職にはその下に管理職がおり、管理職にはチーム リーダーがいて、各チーム リーダーにはその下に従業員がいます。同じ構造を TreeView に表示する必要があります。TreeView のすべてのノードには、従業員の名前とノード内のレコードの数が表示されます (この数はデータベースから取り込まれます)。カウント データは、TreeView の対応するノードの下のテーブル コントロールに表示する必要があります。カウントが 3 レコードの場合、選択したノード ID に基づいて、データベースからデータをフェッチした 3 行のテーブルを表示する必要があります。このテーブルは、ノードの下にのみ存在する必要があります。

コードは次のとおりです。

public class TreeNodeEx : TreeNode
{
    protected override void RenderPostText(HtmlTextWriter writer)
    {
        string connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString().Trim();
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("select * from TableA where Name='Krishna'", con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        Table table1 = new Table();

        int rows = 1;
        int cols = 5;
        for (int j = 0; j < rows; j++)
        {
            TableRow r = new TableRow();
            for (int i = 0; i < cols; i++)
            {
                TableCell c = new TableCell();
                c.Controls.Add(new LiteralControl(ds.Tables[0].Rows[j][i].ToString()));
                r.Cells.Add(c);
            }
            table1.Rows.Add(r);
        }
        table1.RenderControl(writer);
        base.RenderPostText(writer);
    }
}

ノードの下で呼び出されるコードは次のとおりです。

TreeNodeEx n = new TreeNodeEx();    
parent.ChildNodes.Add(n);

カウントを追加するコードは次のとおりです。

child.Text = dr["User_Given_Name"].ToString() + " [Records # (" + dsn.Tables[0].Rows[0][0].ToString() + ")]"; child.Value = dr["User_Email"].ToString().Trim();
4

0 に答える 0