使用repeater tableb绑定数据库

1:前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <table>
        <tr>
    <td align="center">学号</td>
    <td align="center">姓名</td>
    <td align="center">性别</td>
    <td align="center">专业名称</td>
    <td align="center">地址</td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td align="center"><asp:TextBox ID="txtNo" runat="server" Text='<%#Eval("StuId")%>' ReadOnly="true"></asp:TextBox></td>
     <td align="center"><asp:TextBox ID="txtName" runat="server" Text='<%#Eval("StuName")%>' ReadOnly="true"></asp:TextBox></td>
      <td align="center"><asp:TextBox ID="txtSex" runat="server" Text='<%#Eval("StuSex")%>' ReadOnly="true"></asp:TextBox></td>
       <td align="center"><asp:TextBox ID="txtSP" runat="server" Text='<%#Eval("Speciality")%>' ReadOnly="true"></asp:TextBox></td>
        <td align="center"><asp:TextBox ID="txtAddress" runat="server" Text='<%#Eval("StuAddress")%>' ReadOnly="true"></asp:TextBox></td>
    </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
    <tr>
    <td align="center"><asp:TextBox ID="txtNo" runat="server" Text='<%#Eval("StuId")%>' ReadOnly="true"></asp:TextBox></td>
     <td align="center"><asp:TextBox ID="txtName" runat="server" Text='<%#Eval("StuName")%>' ReadOnly="true"></asp:TextBox></td>
      <td align="center"><asp:TextBox ID="txtSex" runat="server" Text='<%#Eval("StuSex")%>' ReadOnly="true"></asp:TextBox></td>
       <td align="center"><asp:TextBox ID="txtSP" runat="server" Text='<%#Eval("Speciality")%>' ReadOnly="true"></asp:TextBox></td>
        <td align="center"><asp:TextBox ID="txtAddress" runat="server" Text='<%#Eval("StuAddress")%>' ReadOnly="true"></asp:TextBox></td>
    </tr>
    </AlternatingItemTemplate>
      <FooterTemplate>
     </table>
   </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>
View Code

2:后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Text;

public partial class Repeater : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        StringBuilder strb = new StringBuilder();
        strb.Append("select t.*");
        strb.Append("from Student t");
        string strConn = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        using (OracleConnection con = new OracleConnection(strConn))
        {
            con.Open();
            using (OracleCommand cmd = new OracleCommand(strb.ToString(), con))
            {
                using (OracleDataAdapter da = new OracleDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Student");
                   Repeater1.DataSource=ds.Tables["Student"];
                   Repeater1.DataBind();
                }

            }

        }
    }
    
}
View Code

3:效果截图:

你可能感兴趣的:(table)