当前位置:Gxlcms > asp.net > asp.net SqlDataReader绑定Repeater

asp.net SqlDataReader绑定Repeater

时间:2021-07-01 10:21:17 帮助过:25人阅读


一直以为不可以,原来是可以的,肤浅啊!
代码如下:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=dnt;user id=ahuinan;pwd=123;");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from dnt_users", conn);
SqlDataReader dr = cmd.ExecuteReader();
rpt.DataSource = dr;
rpt.DataBind();
dr.Close();
conn.Close();
}
</script>

<html>
<head>
<title>SqlDataReader绑定Repeater</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1">
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("uid") %></td>
<td><%#Eval("username") %></td>
<td><%#Eval("password") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>

人气教程排行