0

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

        }
    }

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

有什么建议么......

4

3 回答 3

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
            }
        }
    }
}
于 2013-06-05T07:30:49.123 回答
0

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

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

于 2013-01-08T08:19:09.153 回答
-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 ..
于 2013-01-08T08:34:23.190 回答