从代码隐藏访问列表视图项模板

发布于 2024-12-02 20:00:48 字数 1527 浏览 1 评论 0原文

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

I'm hoping to dynamically change the number of columns in my ItemTemplate of my ListView:

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

Then in the code behind I've got:

 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

Maybe there's another way to go about it, but that's the only idea of got so far. asp.net newbie, any advise, very welcome! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美的太阳 2024-12-09 20:00:49

另一种方法是以相同的方式设置 ItemTemplate,但由于您使用的是数据绑定控件,您可能希望利用列表视图的 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 来完成此类工作,因为它是简单且轻量级的。从我看来,您确实没有利用任何列表视图功能。

Another way would be to set up the ItemTemplate the same way but since you are using a databound control you might want to take advantage of the DataBound event of the listview. This syntax may not be perfect because you may be using a different collection to bind to the list (which would be done after query execution) but this should get you on the right track:

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

I would typically use a Repeater for this type of work since it is bare-bones and lightweight. You really aren't taking advantage of any of the listview features from what i see.

素食主义者 2024-12-09 20:00:49

结合我收到的答案,这就是我要做的工作:

<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%,只需进行一些其他调整,我就可以开始了。谢谢!

Using a combination of the answers I recieved this is what I got to work:

<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
 <ItemTemplate>
   <table>
       <tr>
           <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
       </tr>
   </table>
 </ItemTemplate>
</asp:ListView>

Code Behind:

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

Thanks much for the direction of the other 2 answers, that definately got me 90%, just a couple other tweaks and i was good to go. Thanks!

缺⑴份安定 2024-12-09 20:00:49

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

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

i use this code to hide a element in the item template

visible='<%# Eval("Abstract").ToString().Length == 0 ? false : true %>' 
素罗衫 2024-12-09 20:00:48

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

<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

I think you're going to have to do this a little differently. Try something like this:

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

The syntax might not be perfect, but try doing something like this in your code behind:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文