c# - 将动态生成的 GridView 放入 ASP.Net C# 中的特定 <div>

标签 c# asp.net

我在 C# 的代码隐藏文件中创建了 gridview。

我想将该 GridView 放入特定的 <div> 中源文件。

我生成网格的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class gv1 : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    DataSet ds;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
   {
        cn = new SqlConnection("Data Source=AMIR-PC\\MOHEMMAD;Initial Catalog=CRM_InvestPlus;Integrated Security=True");

   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       cn.Open();
       string query = "select V.Version_Name,S.Software_Name,C.Capacity,V.Price from " +
        "Software_Master S inner join Version_Master V inner join Dealer_License_Capacity C " +
        "on V.Version_ID=C.Version_ID and V.Software_ID=C.Software_ID on " +
        "S.Software_ID=C.Software_ID where C.ID='D00001' and C.Software_ID='001' and C.Version_ID='1'";

    cmd = new SqlCommand(query, cn);
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    cn.Close();

    GridView gr1 = new GridView();
    Table t = new Table();
    gr1.Controls.Add(t);



    int count = Convert.ToInt32(ds.Tables[0].Rows[0]["Capacity"].ToString());

    for (int i = 0; i < count; i++)
    {

        GridViewRow row = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TableCell cell3 = new TableCell();
        TableCell cell4 = new TableCell();


        TextBox txtPrice = new TextBox();

        FileUpload fl = new FileUpload();
        fl.ID = "fl" + i;


        if (i == 0)
        {
            cell1.Text = ds.Tables[0].Rows[0]["Software_Name"].ToString();
            cell2.Text = ds.Tables[0].Rows[0]["Version_Name"].ToString();
            //cell3.Text = ds.Tables[0].Rows[0]["Price"].ToString();
            cell3.Controls.Add(txtPrice);
        }
        else
        {
            cell1.Text = "";
            cell2.Text = "";
            cell3.Text = "";
        }



        cell4.Controls.Add(fl);

        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        row.Cells.Add(cell3);
        row.Cells.Add(cell4);

        gr1.Controls[0].Controls.AddAt(i, row);



    }
    form1.Controls.Add(gr1);
   }
}

网格生成成功...

现在我想将网格 gr1 放置到 <div class="block-border">标签是:

 <div class="grid_6">   
        <div class="container_12">
            <div class="block-border">

怎么可能把 gr1 放在最后 <div> ..???

请帮忙
提前致谢。

最佳答案

你需要添加 runat="server"和一个 id 到你的 div 标签,你可以这样做

<div id="block" runat="server" class="block-border">

block.Controls.Add(gr1);

关于c# - 将动态生成的 GridView 放入 ASP.Net C# 中的特定 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766488/

相关文章:

c# - 在我的示例中使用什么数据结构

c# - 如何拒绝玩家移动某条路径?

javascript - 如何用后面的vb.net代码读取HTML5 textarea文本

javascript - 更改 FullCalendar 中的日期背景颜色

c# - 在数据更改时异步更新 asp.net 中继器

ASP.NET CustomErrors 在 global.asax 或 web.config 中发生异常时不会触发

C#时间管理问题

c# - 如何防止用户登录多个设备以及如何从之前的设备结束 session ?

c# - 通用类型作为变量

c# - 动态创建一个 ImageButton