如何动态地在占位符中添加两个以上的控件

时间:2014-01-02 06:39:54

标签: c# asp.net

Hello Everyone我想在动态生成的每个gridview上添加一个标签。请告诉我如何在每个gridview中添加标签我正在使用此代码

  private void CreateGrid()
    {
        DataSet dsServiceId;
        dsServiceId = FetchServiceId();
        int countServices;
        countServices = dsServiceId.Tables[0].Rows.Count;
        //----
        Table t = new Table();
        TableRow row = new TableRow();
        //---
        for (int i = 0; i < countServices; i++)
        {
            int serviceid = Convert.ToInt32(dsServiceId.Tables[0].Rows[i]["pServiceID"].ToString());
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ezyMobile"].ConnectionString;
            SqlCommand cmd = new SqlCommand("Ezy_opWiseSaleAll", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@date", DateTime.Now.ToString());
            cmd.Parameters.AddWithValue("@Serviceid", serviceid);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView gv = new GridView();
            gv.HeaderStyle.BackColor = Color.Gray;
            gv.HeaderStyle.ForeColor = Color.White;
            gv.ID = "_gridview" + i;
            gv.DataSource = ds;
            gv.DataBind();
            //-----
            if (i % 4 == 0)
            {
                row = new TableRow();
            }
            TableCell cell = new TableCell();
            cell.Controls.Add(gv);
            row.Controls.Add(cell);
            t.Controls.Add(row);
            PlaceHolder1.Controls.Add(t);
        //------
    }
    }
    protected DataSet FetchServiceId()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ezyMobile"].ConnectionString;
        SqlCommand cmd = new SqlCommand("select distinct pServiceID from tbProcTransactions",con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;

    }

我想添加一些有关动态生成的gridview的信息。 感谢

1 个答案:

答案 0 :(得分:0)

你到底感到困惑的是什么?向占位符添加标签将遵循用于添加Table控件的相同类型的模式。

所以,在你添加你创建的表之后,你会这样做:

...
PlaceHolder1.Controls.Add(t);

Label myLabel = new Label(); // create a new label
myLabel.Text = "Some stuff about this GridView"; // add some text
PlaceHolder1.Controls.Add(myLabel); // add it to the PlaceHolder