简体   繁体   中英

How to display data in Repeater using asp.net webforms?

I have to display data from database like in a picture in asp.net webforms. I tried to do it through Repeater ( second picture ):

<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnection %>" SelectCommand="DataSelect" SelectCommandType="StoredProcedure"></asp:SqlDataSource>                    
                <asp:Repeater ID="rptStudents" runat="server" DataSourceID="SqlDataSource" OnItemDataBound="rptStudents_ItemDataBound">
                    <HeaderTemplate>
                        <asp:Table runat="server">
                            <asp:TableRow>
                                <asp:TableCell>Course</asp:TableCell>
                                <asp:TableCell>Grade</asp:TableCell>
                            </asp:TableRow>
                        </asp:Table>                                                         
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Table ID="tbStudents" runat="server">
                            <asp:TableRow>
                                <asp:TableCell ID="tcStudent" runat="server" ColumnSpan="2" Visible="true">
                                    <%#DataBinder.Eval(Container.DataItem, "StudentName") %>
                                </asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell>
                                    <%#DataBinder.Eval(Container.DataItem, "CourseName") %>
                                </asp:TableCell>
                                <asp:TableCell>
                                    <%#DataBinder.Eval(Container.DataItem, "Grade") %>
                                </asp:TableCell>
                            </asp:TableRow>
                        </asp:Table>                            
                    </ItemTemplate>
                </asp:Repeater>

What can be added to rptStudents_ItemDataBound or there is another function that can be used? By default tcStudent is visible but if tcStudent.Visible = false; then all students names not displayed. But how to make that student's name is displayed only once?

protected void rptStudents_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        TableCell tcStudent = (TableCell)e.Item.FindControl("tcStudent");
        tcStudent.Visible = false;            
    }

I just started to learn asp.net webforms, please help. Thanks.

A repeater is an option, but if you simply want a grid, consider using a GridView. You can then alter the formatting in the tags to make the header style and alternating row style. Far less complex.

NOTE: Good developers are lazy! (I mean efficient, of course?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM