DataSet与控件的灵活应用

 SqlConnection con = smile.database.DatabaseCommon.CreateCon();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select Role_ID,Role_Name from [Role] order by Role_ID asc";
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        cmd.Connection.Open();
        da.Fill(ds, "Role");
        DataTable Role_datatable = ds.Tables["Role"];
        cmd.Connection.Close();

        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select Act_ID,Act_Name from View_FunAct where Func_ID=@Func_ID order by Act_ID asc";
        cmd.Parameters.AddWithValue("@Func_ID", DropdownList_Function1.SelectedValue);
        da.SelectCommand = cmd;
        cmd.Connection.Open();
        da.Fill(ds, "Action");
        DataTable Act_datatable = ds.Tables["Action"];
        cmd.Connection.Close();

        foreach (DataRow Role in Role_datatable.Rows)
        {
            Label I = new Label();
            I.ID = "Role_" + Role["Role_ID"].ToString();
            I.Text = "<br/>" + Role["Role_Name"].ToString();
            I.ForeColor = System.Drawing.Color.Red;
            CheckBoxList cbl = new CheckBoxList();
            cbl.ID = "Action_" + Role["Role_ID"].ToString();
            cbl.RepeatDirection = RepeatDirection.Horizontal;
              foreach (DataRow Action in Act_datatable.Rows)
            {
                ListItem li = new ListItem();
                li.Text = Action["Act_Name"].ToString();
                li.Value = Action["Act_ID"].ToString();
                cbl.Items.Add(li);
 
            }
            this.Panel1.Controls.Add(I);
            this.Panel1.Controls.Add(cbl);
         }
       很有意思的一段代码。。。嗄嗄。。

posted on 2008-03-01 15:07  prayforsmile  阅读(340)  评论(0编辑  收藏  举报