Zugriff auf listview itemtemplate aus codebehind

Ich hoffe, dynamisch ändern Sie die Anzahl der Spalten in meiner ItemTemplate meiner 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>

Dann in der code-behind habe ich:

 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

Vielleicht gibt es einen anderen Weg zu gehen, aber das ist die einzige Idee, der so weit gekommen. asp.net Neuling, jede Beratung sehr herzlich willkommen! Danke!

InformationsquelleAutor russds | 2011-09-02
Schreibe einen Kommentar