从后面的代码访问listview itemtemplate

时间:2011-09-02 17:11:59

标签: asp.net vb.net listview itemtemplate

我希望动态更改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新手,任何建议,非常欢迎!谢谢!

4 个答案:

答案 0 :(得分:2)

我认为你必须以不同的方式做到这一点。尝试这样的事情:

<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

答案 1 :(得分:2)

另一种方法是以相同的方式设置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

我通常会使用Repeater进行此类工作,因为它很简单且轻巧。你真的没有利用我所看到的任何listview功能。

答案 2 :(得分:1)

使用我收到的答案的组合,这就是我的工作:

<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%,只是其他几个调整,我很高兴。谢谢!

答案 3 :(得分:1)

我使用此代码隐藏项目模板中的元素

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