如何使用asp.net webforms在Repeater中显示数据?

时间:2015-08-13 16:36:35

标签: c# asp.net webforms

我必须在asp.net webforms中的picture 中显示来自数据库的数据。我尝试通过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>

什么可以添加到 rptStudents_ItemDataBound 或者还有其他可以使用的功能?默认情况下, tcStudent 可见,但如果 tcStudent.Visible = false; ,则不显示所有学生姓名。但是如何让这个学生的名字只显示一次?

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

我刚开始学习asp.net webforms,请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

转发器是一个选项,但如果您只是想要一个网格,请考虑使用GridView。然后,您可以更改标记中的格式以生成标题样式和交替行样式。远不那么复杂。

注意:优秀的开发人员很懒惰! (当然,我的意思是效率很高?)

相关问题