Repeater Control中的ASP.NET GridView

时间:2014-02-10 16:39:51

标签: asp.net gridview repeater

我检查了所有可用文章,似乎没有人帮助我。

我有一个位于Repeater控件内的GridView。填充Gridview的数据是动态的,并按SectionID分组。我需要为每个重复的GridView列出根据其SectionID分组的数据行。

我该怎么做?

感谢。

这是我到目前为止所做的:

Public Sub GrabRepeaterData()
    Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
    Dim ss As New SqlConnection(connstr)
    Dim sqlStr As String = "SELECT SectionID, Name FROM Table1"
    Dim selectCMD As New SqlClient.SqlCommand(sqlStr, ss)
    Dim dt As New DataTable
    Dim ds As New DataSet
    Dim dataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
    dataAdapter.SelectCommand = selectCMD
    dataAdapter.Fill(dt)
    selectCMD.Dispose()
    dataAdapter.Dispose()
    ss.Close()
    ss.Dispose()

    dt.Columns.Add("Column1")
    dt.Columns.Add("Column2")
    dt.Columns.Add("Column3")
    dt.Columns.Add("Column4")
    dt.Columns.Add("Column5")
    dt.Columns.Add("Column6")
    dt.Columns.Add("Column7")
    dt.Columns.Add("Column8")

    For Each row As DataRow In dt.Rows
        Dim SectionID As String = ""
        SectionID = row("SectionID")

        Dim Column1 As String = ""
        Dim Column2 As String = ""
        Dim Column3 As Boolean
        Dim Column4 As String = ""
        Dim Column5 As String = ""
        Dim Column6 As String = ""
        Dim Column7 As Boolean
        Dim Column8 As Boolean
        Dim ProgConn As String = ""
        ProgConn = ConfigurationManager.ConnectionStrings("MyString").ToString()
        Dim ProgSqlQuery As New SqlConnection(ProgConn)
        Dim ProgResults As New SqlCommand("SELECT [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8] FROM Table2 WHERE SectionID = @SectionID ORDER BY Column4 DESC", ProgSqlQuery)
        ProgResults.Parameters.AddWithValue("@SectionID", SectionID).Value = SectionID
        ProgSqlQuery.Open()
        Dim rProg As SqlDataReader = ProgResults.ExecuteReader()
        While rProg.Read()
            If Not rProg("Column1").Equals(DBNull.Value) Then
                Column1 = CStr(rProg("Column1"))
            End If
            If Not rProg("Column2").Equals(DBNull.Value) Then
                Column2 = CStr(rProg("Column2"))
            End If
            If Not rProg("Column3").Equals(DBNull.Value) Then
                Column3 = CStr(rProg("Column3"))
            End If
            If Not rProg("Column4").Equals(DBNull.Value) Then
                Column4 = CStr(rProg("Column4"))
            End If
            If Not rProg("Column5").Equals(DBNull.Value) Then
                Column5 = CStr(rProg("Column5"))
            End If
            If Not rProg("Column6").Equals(DBNull.Value) Then
                Column6 = CStr(rProg("Column6"))
            End If
            If Not rProg("Column7").Equals(DBNull.Value) Then
                Column7 = CStr(rProg("Column7"))
            End If
            If Not rProg("Column8").Equals(DBNull.Value) Then
                Column8 = CStr(rProg("Column8"))
            End If
        End While
        rProg.Close()
        ProgResults.Dispose()
        ProgSqlQuery.Close()
        ProgSqlQuery.Dispose()

        row("Column1") = Column1
        row("Column2") = Column2
        row("Column3") = Column3
        row("Column4") = Column4
        row("Column5") = Column5
        row("Column6") = Column6
        row("Column7") = Column7
        row("Column8") = Column8

        row.EndEdit()
        dt.AcceptChanges()
    Next


    CustomInfoRepeater.DataSource = dt
    CustomInfoRepeater.DataBind()


End Sub

Protected Sub CustomInfoRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles CustomInfoRepeater.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Then
        Try

            Dim grdVw As GridView = TryCast(e.Item.FindControl("CustomInfoGridView"), GridView)
            grdVw.DataSource = DirectCast(e.Item.DataItem, DataTable).Rows
            grdVw.DataBind()
            Dim CustPanel As UpdatePanel = DirectCast(CustomInfoRepeater.Items(0).FindControl("CustomInfoPanel"), UpdatePanel)
            CustPanel.Update()

        Catch
        End Try
    End If
End Sub

这是我的HTML:

<asp:Repeater ID="CustomInfoRepeater" runat="server">
    <ItemTemplate>
        <div class="download-box3">               

                </h2>
            <asp:UpdatePanel ID="CustomInfoPanel" runat="server" UpdateMode="Conditional">
                <Triggers>
                    <asp:PostBackTrigger ControlID="CustomInfoGridView" />
                </Triggers>
                <ContentTemplate>
                    <asp:GridView ID="CustomInfoGridView" runat="server" Width="100%" AutoGenerateColumns="False"
                        DataKeyNames="Column1" GridLines="None" RowStyle-Height="40px" EnableViewState="False"
                        CellPadding="4" ForeColor="#333333" RowStyle-VerticalAlign="Middle">
                        <Columns>
                            <asp:BoundField DataField="Column1" />
                            <asp:TemplateField ItemStyle-Width="35px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
                                <ItemTemplate>
                                    <asp:ImageButton ID="newIcon" runat="server" Width="31px" CssClass="myGridImage"
                                        Visible="false" ImageUrl="images/new.png" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="35px" />
                            </asp:TemplateField>
                            <asp:TemplateField ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
                                <ItemTemplate>
                                    <asp:ImageButton ID="DeleteButton" runat="server" AlternateText="Delete" ImageUrl="images/xmark.png"
                                        OnClientClick="return confirm('Are you sure you want to delete this entry?')"
                                        CommandArgument='<%# Eval("Column1") %>' CommandName="Remove" CssClass="myGridImage">
                                    </asp:ImageButton>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30px" />
                            </asp:TemplateField>
                            <asp:TemplateField ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
                                <ItemTemplate>
                                    <asp:LinkButton ID="GenComUpdateButton" runat="server" CssClass="buttonsmall" Text="update"
                                        CommandArgument='<%# Eval("Column1") %>' CommandName="GenComments"></asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Impact" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="30px"
                                ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
                                <ItemTemplate>
                                    <asp:Image ID="statusIcon" runat="server" Width="20px" CssClass="myGridImage" Visible="false"
                                        ImageUrl="" AlternateText="*" />
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                            </asp:TemplateField>
                            <asp:TemplateField ItemStyle-Width="50px" HeaderText="Files" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-VerticalAlign="Middle">
                                <ItemTemplate>
                                    <asp:ImageButton ID="filesIcon" runat="server" Width="23px" CssClass="myGridImage"
                                        CommandArgument='<%# Eval("Column3") %>' CommandName="Download" Visible="false"
                                        ImageUrl="images/pdf.png" AlternateText="*" />
                                    <asp:LinkButton ID="AttachAssetsBtn" runat="server" CssClass="buttonredsmall" Text="upload"
                                        CommandArgument='<%# Eval("Column1") %>' CommandName="Uploads" Visible="false"></asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="Column3" HeaderText="Summary" HeaderStyle-HorizontalAlign="Center"
                                ItemStyle-VerticalAlign="Middle">
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                                <ItemStyle VerticalAlign="Middle" HorizontalAlign="Left" />
                            </asp:BoundField>
                            <asp:TemplateField ItemStyle-Width="50px" HeaderText="Reviewed" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-VerticalAlign="Middle">
                                <ItemTemplate>
                                    <asp:Image ID="reviewedIcon" runat="server" Width="23px" CssClass="myGridImage" Visible="false"
                                        ImageUrl="images/checkmark.png" AlternateText="*" />
                                    <asp:CheckBox ID="reviewedCheckBox" runat="server" CssClass="inputtext" Visible="false"
                                        AutoPostBack="true" OnCheckedChanged="GenComCheckUpdate" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="Column4" HeaderText="Updated" HeaderStyle-HorizontalAlign="Center"
                                ItemStyle-VerticalAlign="Middle" ItemStyle-Width="75px">
                                <ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Column6" Visible="false" />
                            <asp:BoundField DataField="Column7" Visible="false" />
                        </Columns>
                        <EmptyDataTemplate>
                            <span class="data-none">No Comments have been added to this section.</span>
                        </EmptyDataTemplate>
                        <RowStyle CssClass="RowStyle" BackColor="#F7F6F3" ForeColor="#333333" />
                        <EmptyDataRowStyle CssClass="EmptyRowStyle" />
                        <PagerStyle CssClass="PagerStyle" BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle CssClass="SelectedRowStyle" BackColor="#E2DED6" Font-Bold="True"
                            ForeColor="#333333" />
                        <HeaderStyle CssClass="HeaderStyle" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle CssClass="EditRowStyle" BackColor="#999999" />
                        <AlternatingRowStyle CssClass="AltRowStyle" BackColor="White" ForeColor="#284775" />
                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
</asp:Repeater>

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我从代码中理解的是,您将两个数据源合并为一个。然后将它绑定到Repeater,然后从转发器DataItem获取GV的数据源并将其绑定到GridView。

我建议将数据源分开并分两步完成:

1:将转发器绑定到SectionID,即SELECT SectionID,Name FROM Table1返回的数据

2:在Repeater ItemDataBound中,获取SectionID并使用ProgResults中的查询获取数据。然后将其绑定到GV。

答案 1 :(得分:0)

我完全赞同gbs。这是你如何做到的:

在标记中,在更新面板外部的转发器中添加隐藏字段:

<asp:Repeater ID="CustomInfoRepeater" runat="server">
    <ItemTemplate>
        <asp:HiddenField ID="hdnSectionID" Value='<%# Eval("SectionID")%>' runat="server" />
        <asp:Label ID="lblName" Text='<%# Eval("Name")%>' runat="server"></asp:Label>
        <div class="download-box3">
            </h2>
<asp:UpdatePanel ID="CustomInfoPanel" runat="server" UpdateMode="Conditional">
<%-- Rest of the markup goes here --%>

仅重写GrabRepeaterData() sub以填充转发器:

Public Sub GrabRepeaterData()
    Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
    Dim dt As New DataTable

    Using conn As SqlConnection = New SqlConnection(connstr)
        conn.Open()
        Dim sqlStr As String = "SELECT SectionID, Name FROM Table1"
        Dim cmd As New SqlClient.SqlCommand(sqlStr, conn)
        Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
        adapter.SelectCommand = cmd
        adapter.Fill(dt)
    End Using

    CustomInfoRepeater.DataSource = dt
    CustomInfoRepeater.DataBind()

End Sub

在转发器的ItemDataBound中,找到HiddenField,找到它的值,在GridView内找到UpdatePanel,以这种方式填充:

Protected Sub CustomInfoRepeater_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles CustomInfoRepeater.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Then
        Dim sectionID As Integer = 0
        Dim hdnSectionID = TryCast(e.Item.FindControl("hdnSectionID"), HiddenField)
        Dim CustomInfoPanel = TryCast(e.Item.FindControl("CustomInfoPanel"), UpdatePanel)

        If (CustomInfoPanel IsNot Nothing AndAlso hdnSectionID IsNot Nothing AndAlso Integer.TryParse(hdnSectionID.Value, sectionID)) Then

            Dim CustomInfoGridView = TryCast(CustomInfoPanel.FindControl("CustomInfoGridView"), GridView)

            If (CustomInfoGridView IsNot Nothing) Then
                Dim connstr As String = ConfigurationManager.ConnectionStrings("MyString").ToString()
                Dim dt As New DataTable

                Using conn As SqlConnection = New SqlConnection(connstr)
                    conn.Open()
                    Dim cmd As New SqlCommand("SELECT [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8] FROM Table2 WHERE SectionID = @SectionID ORDER BY Column4 DESC", conn)
                    cmd.Parameters.AddWithValue("@SectionID", sectionID)
                    Dim adapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
                    adapter.SelectCommand = cmd
                    adapter.Fill(dt)
                End Using
                CustomInfoGridView.DataSource = dt
                CustomInfoGridView.DataBind()
            End If

        End If
    End If
End Sub