【问题标题】:Link button to display grid view asp.net dynamically链接按钮以动态显示网格视图 asp.net
【发布时间】:2013-09-03 12:25:30
【问题描述】:

我要显示n网格,n是可变的,那我不知道我会有多少网格。

我的问题是,我必须用 Visible false 来初始化这个网格,当点击一个按钮时会显示该按钮的特定网格,那么我怎样才能将一个按钮链接到一个网格视图?

我生成网格的代码:

    foreach (List<DataRow> lst in grids)
    {

        dt = lst.CopyToDataTable();

        GridView grv = new GridView();
        grv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromName("#cccccc");
        grv.HeaderStyle.BackColor = System.Drawing.Color.Gray;
        grv.ID = "grid_view"+i;
        grv.Visible = false;
        grv.DataSource = dt;
        grv.DataBind();


        Label lblBlankLines = new Label();
        lblBlankLines.Text = "<br /><br />";


        Label lblTipo = new Label();
        string tipoOcorrencia = lst[0]["DESC_OCORRENCIA"].ToString();
        tipoOcorrencia = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(tipoOcorrencia);
        int quantidade = lst.Count;
        lblTipo.Text = tipoOcorrencia + ": " + quantidade;


        LinkButton lkBtn = new LinkButton();
        lkBtn.ID = "link_button"+i;
        lkBtn.Text = "+";

        place_grids.Controls.Add(lblBlankLines);
        place_grids.Controls.Add(lkBtn);
        place_grids.Controls.Add(lblTipo);
        place_grids.Controls.Add(grv);


        place_grids.DataBind();

        i++;
    }

提前致谢。

【问题讨论】:

  • 你在哪里添加链接按钮?
  • 我要补充,但我不知道正确的做法。
  • 一次只能绑定一个网格。当您单击按钮时,它将根据索引值绑定新网格。

标签: c# asp.net gridview dynamic-data


【解决方案1】:

如下修改你的 foreach 循环。

private void GenerateControls()
{
    int i = 0;
    foreach (List<DataRow> lst in grids)
    {
        dt = lst.CopyToDataTable();

        GridView grv = new GridView();
        grv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromName("#cccccc");
        grv.HeaderStyle.BackColor = System.Drawing.Color.Gray;
        grv.ID = "grid_view" + i;
        //grv.Visible = false;//Commented as the grid needs be generated on client side, in order to make it visible from JavaScript/jQuery
        grv.Attributes.Add("style", "display:none;");
        grv.DataSource = dt;
        grv.DataBind();

        //Adding dynamic link button
        LinkButton lnkButton = new LinkButton();
        lnkButton.Text = "button " + i;
        //lnkButton.Click += new EventHandler(lnkButton_Click);
        lnkButton.ID = "lnkButton" + i;
        lnkButton.OnClientClick = "ShowGrid('" + grv.ClientID + "');";

        Label lblTipo = new Label();
        lblTipo.Text = "text " + i;
        lblTipo.ID = "lbl" + i;

        tempPanel.Controls.Add(lblTipo);
        tempPanel.Controls.Add(grv);
        tempPanel.Controls.Add(lnkButton);

        tempPanel.DataBind();
        i++;
    }
}

然后,如果您希望服务器端事件触发,则必须添加如下链接按钮单击事件。 (取消注释将事件处理程序分配给链接按钮的行。)

protected void lnkButton_Click(Object sender, EventArgs e)
{
    LinkButton lnkButton = (LinkButton)sender;
    String index = lnkButton.ID.Substring(lnkButton.ID.Length - 1);

    GridView grv = (GridView)tempPanel.FindControl("grid_view" + index);
    grv.Visible = true;
}

您需要在 Page_Init 事件中添加所有动态添加的控件以维护它们的状态。参考下面的链接可能会有用。

Dynamically Created Controls losing data after postback

ViewState in Dynamic Control

Page_Init事件调用方法GenerateControls如下。

protected void Page_Init(object sender, EventArgs e)
{
    GenerateControls();
}

编辑:

JavaScript 函数...

function ShowGrid(gridID) {
    document.getElementById(gridID).style.display = ''
}

我保持服务器端点击事件不变。但是我已经注释了将事件处理程序分配给链接按钮的行。

【讨论】:

  • 我正在尝试使用 JavaScript 执行此操作,因为我不想每次都刷新数据。
  • @guisantogui 检查我编辑的答案,其中包括 JavaScript 函数和 foreach 循环中的两行代码,以便 JavaScript 函数工作。
猜你喜欢
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
  • 2015-06-26
相关资源
最近更新 更多