【问题标题】:Binding listView column dynamically in asp.net在asp.net中动态绑定listView列
【发布时间】:2012-12-22 00:55:49
【问题描述】:

我需要创建一个 listView,其中的列数将在运行时发生变化。

我的aspx页面代码:

<asp:ListView runat="server" ID="ReportListView">
    <LayoutTemplate>
        <table>
            <tr>
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <table>
            <tr>
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder1" />
            </tr>
        </table>
    </ItemTemplate>
</asp:ListView>

在 ItemTemplate 中,我需要从后面的代码中动态绑定列。

我的 .cs 页面代码:

SqlCommand cmd = new SqlCommand(query);

    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);

                ReportListView.DataSource = ds;
                ReportListView.DataBind();
            }
        }
    }

    foreach (ListViewDataItem listItem in ReportListView.Items)
    {
        PlaceHolder plc = (PlaceHolder)listItem.FindControl("itemPlaceHolder1");
        if (plc != null)
        {
                Literal ltrl = new Literal();
                ltrl.Text = "<td>" + listItem.DataItem + "</td>";
                plc.Controls.Add(ltrl);

        }
    }

但它在浏览器上什么也不返回。没有错误也没有输出....

任何建议......

【问题讨论】:

  • 您是否检查过您的“ds”是否充满了所需的数据?
  • 是的..“ds”包含数据...但它没有显示出来

标签: asp.net listview binding itemtemplate


【解决方案1】:

如果您只是在构建具有一组动态列的表之后,为什么不使用GridView 并创建您自己的ColumnsGenerator?创建一个实现IAutoFieldGenerator 的类,该类将返回要创建的列集,并将其分配给GridView.ColumnsGenerator 属性。

如果您真的想采用ListView 和模板的方式,您可以创建一个继承IBindableTemplate 的类并将其分配给ListView.ItemTemplate

【讨论】:

  • 我需要它在 listView... 你能发布一些关于如何使用 IBindableTemplate 类的示例代码
【解决方案2】:

我在上面写了一个帖子:http://start-coding.blogspot.com/2013/06/dynamic-columns-in-listview.html

在 ItemDataBound 事件上,执行如下操作(您可以替换为其他控件或 LoadControl):

private void dynamicPopulateRow(HtmlTableRow row, System.Data.DataRowView drv, int iGeneration)
{
    if (row != null)
    {
        // http://www.pcreview.co.uk/forums/do-enumerate-all-columns-dataviewrow-t1244448.html
        foreach (DataColumn dc in drv.Row.Table.Columns)
        {
            string sEmployeeID = drv["LoginID"].ToString();

            if (dc.ColumnName.Equals("LoginID"))
            {
                // http://msdn.microsoft.com/en-US/library/e5daxzcy(v=vs.80).aspx
                // Define a new HtmlTableCell control.
                HtmlTableCell cell = new HtmlTableCell("td");

                // Create the text for the cell.
                cell.Controls.Add(new LiteralControl(Convert.ToString(drv[dc.ColumnName])));
                cell.ColSpan = dc.ColumnName.Equals("LoginID") ? I_COLSPAN - iGeneration : 1;

                // Add the cell to the HtmlTableRow Cells collection. 
                row.Cells.Add(cell);
            }
            else if (!(dc.ColumnName.Equals("GENERATION") ||
                        dc.ColumnName.Equals("hierarchy") ||
                        dc.ColumnName.Equals("rowNo") ||
                        dc.ColumnName.Equals("EmployeeID")))
            {
                // http://msdn.microsoft.com/en-US/library/e5daxzcy(v=vs.80).aspx
                // Define a new HtmlTableCell control.
                HtmlTableCell cell = new HtmlTableCell("td");

                bool bIsNull = drv[dc.ColumnName] is System.DBNull;

                Literal ltrl = new Literal();
                ltrl.Text += "<input type=\"checkbox\" name=\"" + dc.ColumnName + "\"" +
                                (bIsNull ? "" : " value=" + drv[dc.ColumnName].ToString()) +
                                " id=\"" + sEmployeeID + "~" + dc.ColumnName.Replace(" ", "_") + "\"" +//will be retrieved later
                                " onclick=\"didModify(this)\" " +
                                (bIsNull ? " disabled" : "") +
                                (!bIsNull && ((int)drv[dc.ColumnName]) > 0 ? " checked>" : ">");

                cell.Controls.Add(ltrl);
                // Add the cell to the HtmlTableRow Cells collection. 
                row.Cells.Add(cell);
            }
            else
            {
                //other rows
            }
        }
    }
}

【讨论】:

  • 嗨,Gary,我对你的示例很感兴趣,但我很难理解,因为它看起来很复杂,请你简化你的代码,谢谢
【解决方案3】:
protected void methodone()
{
  SqlCommand cmd = new SqlCommand(query);
  SqlConnection con = new SqlConnection(conString);
  DataSet ds = new DataSet()
  try
  {

  SqlDataAdapter sda = new SqlDataAdapter(cmd,con);
  sda.Fill(ds);
  ReportListView.DataSource = ds;
  ReportListView.DataBind();
}
catch(SqlException ex)
{
  throw;
}
finally
{
  if(con!=null)
   con.close();
}
}

//and dont foreget to call the methodone at the page load ..wherever ..

【讨论】:

  • 你在调试的时候检查过你的ds是否被填满了吗?你检查你的查询了吗? listview 控件是否可见?
  • 哈哈..删除占位符并在普通表单上运行它并检查..可能是你弄乱了占位符
  • 是的.. 'ds' 包含数据...我尝试删除占位符但没有输出....
  • 听起来很奇怪 renu :-( 如果你有时间试试 grid..我会试着告诉你早上..我今天要离开
  • 我在 ListView 控件的 'ItemDataBound' 函数中尝试了占位符代码。它将值绑定到 listView... 但是列名没有绑定到它...
猜你喜欢
  • 2011-05-19
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2010-12-07
相关资源
最近更新 更多