asp.net Repeater控件简单例子

MessageOnline.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MessageOnline.aspx.cs" Inherits="WebApplication1.MessageOnline" %>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>title>
head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <table>
                        <tr>
                            <td>编号td>
                            <td>用户名td>
                            <td>用户密码td>
                            <td>用户类型td>
                        tr>
                HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td><%#Eval("id") %>td>
                        <td><%#Eval("username") %>td>
                        <td><%#Eval("userpwd") %>td>
                        <td><%#Eval("usertype") %>td>
                    tr>
                ItemTemplate>
                <FooterTemplate>
                    table>
                FooterTemplate>
            asp:Repeater>
        div>
    form>
body>
html>

上面代码Eval中string值为数据库中字段名
MessageOnline.aspx.cs

string connStr = "Data Source=.;Initial Catalog=webServer;Integrated Security=True";
string sql = string.Format("select * from [Admin]");
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);

Repeater1.DataSource = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Repeater1.DataBind();

Repeater控件
HeaderTemplate为页眉
ItemTemplate用于显示数据(就是个封装的方法 asp,java等方面语言的数据显示需要用循环输出 但这个标签里面只需要绑定一个数据库字段)
FooterTemplate为页脚

你可能感兴趣的:(asp.net)