我需要创建一个 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);
}
}
但它在浏览器上什么也不返回。没有错误也没有输出....
有什么建议么......