asp.net 从CodeBehind访问ListView ItemTemplate

xqkwcwgp  于 2023-06-07  发布在  .NET
关注(0)|答案(5)|浏览(170)

我希望动态地改变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,有什么建议,非常欢迎!谢谢!

bbmckpt7

bbmckpt71#

我觉得你得换个方式了。试试这样的东西:

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

vuv7lop32#

另一种方法是以相同的方式设置ItemTemplate,但由于您使用的是数据绑定控件,因此您可能希望利用listview的DataBound事件。这种语法可能并不完美,因为你可能使用了不同的集合来绑定到列表(这将在查询执行后完成),但这应该会让你走上正确的道路:

Protected Sub ReportListView_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ReportListView.ItemDataBound
    If (e.Item.ItemType = ListViewItemType.DataItem) Then
        Dim di As Data.DataRowView = e.Item.DataItem
        Dim plc As PlaceHolder = DirectCast(e.Item.FindControl("PlaceHolder1"), PlaceHolder)

        For Each c In di.Row.ItemArray
            Dim l As New Literal
            l.Text = String.Format("<td>{0}</td>", c.ToString)
            plc.Controls.Add(l)
        Next

    End If
End Sub

我通常会使用一个中继器为这种类型的工作,因为它是裸露的骨头和轻量级。从我所看到的,你真的没有利用任何列表视图功能。

nfg76nw0

nfg76nw03#

使用我收到的答案的组合,这是我要做的:

<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
 <ItemTemplate>
   <table>
       <tr>
           <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
       </tr>
   </table>
 </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.

 ' this seems to actually run the sql, and bind the results.  If i don't run the function DataBind(), it's as if the sql never runs.
 ReportListView.DataBind() 

 ' the rest of the work is done in the next function.
End Sub

Protected Sub ReportListView_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ReportListView.ItemDataBound

    If (e.Item.ItemType = ListViewItemType.DataItem) Then
        Dim plc As PlaceHolder = DirectCast(e.Item.FindControl("itemPlaceHolder"), PlaceHolder)
        Dim di As Data.DataRowView = e.Item.DataItem()

        For Each c In di.Row.ItemArray
            Dim l As New Literal
            l.Text = String.Format("<td class='customreport'>{0}</td>", c.ToString)
            plc.Controls.Add(l)
        Next

    End If
End Sub

非常感谢其他2个答案的方向,这肯定让我90%,只是其他几个调整,我很好去。谢谢!

vatpfxk5

vatpfxk54#

我用这段代码来隐藏项目模板中的元素

visible='<%# Eval("Abstract").ToString().Length == 0 ? false : true %>'
shyt4zoc

shyt4zoc5#

我有一个ListView类似的例子与动态控件添加占位符。我无法在页面保存中恢复值,有人能帮忙吗?

相关问题