在asp.net中动态绑定listView列

时间:2013-01-08 07:52:03

标签: asp.net listview binding itemtemplate

我需要创建一个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);

        }
    }

但它在浏览器上没有返回任何内容。没有错误,也没有输出......

任何建议......

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我写了一篇文章: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
            }
        }
    }
}

答案 2 :(得分:-1)

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 ..