gpt4 book ai didi

asp.net - 从代码隐藏访问 ListView 项模板

转载 作者:行者123 更新时间:2023-12-05 01:10:00 24 4
gpt4 key购买 nike

我希望动态更改 ListView 的 ItemTemplate 中的列数:

<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate>
<table>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<!-- need to dynamically create the number of columns in the code behind
based on the select statement in the SelectCommand -->
</ItemTemplate>
</asp:ListView>

然后在后面的代码中我得到:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Based on The Request.Form variables sent, the SQL command will
' select x number of columns:
' SqlStatement = "SELECT " for each Request.Form values " FROM staff"
' example: "SELECT Fname, Lname, email, phone FROM staff"
' Run sql command.

' that all works, the question now is how do i change the ItemTemplate
' so that it has the correct number of columns. Something like this: (pseudo code)
For each row found in sql command
ReportListView.ItemTemplate.Add( "<tr>" )
For each Request.Form as i
ReportLIstView.ItemTemplate.Add( "<td>" & sqlresult(i) & "</td>" )
Next
ReportListView.ItemTemplate.Add( "</tr>" )
Next
End Sub

也许还有另一种方法可以解决这个问题,但这是迄今为止唯一的想法。 ASP.NET新手,有什么建议,非常欢迎!谢谢!

最佳答案

我认为你必须采取一些不同的做法。尝试这样的事情:

<table>
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<tr>
<asp:PlaceHolder Id="PlaceHolder1" runat="server" />
</tr>
</ItemTemplate>
</asp:ListView>
</table>

语法可能并不完美,但请尝试在后面的代码中执行类似的操作:

For Each listItem As ListViewDataItem In ListView1.Items
Dim plc As PlaceHolder = DirectCast(listItem.FindControl("PlaceHolder1"), PlaceHolder)
If plc IsNot Nothing Then
For Each item As String In Request.Form
plc.Controls.Add(New Literal(String.Format("<td>{0}</td>", item)))
Next
End If
Next

关于asp.net - 从代码隐藏访问 ListView 项模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com