沒有执行.aspx代码,应该是执行完Repeater1.DataBind();后再执行aspx文件中Eval那里才对啊,为什么直接就执行con.Close();
.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<asp:HeaderTemplate>
<table>
<tr>
<td>
编号</td>
<td>
身份证号码</td>
<td>
姓名</td>
</tr>
</asp:HeaderTemplate>
<asp:ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="bianhao" Text='<%# Eval("员工编号") %>'/>
</td>
<td>
<asp:Label runat="server" ID="shenfenzheng" Text='<%# Eval("身份证号码") %>'/>
</td>
<td>
<asp:Label runat="server" ID="xingming" Text='<%# Eval("学生姓名") %>'/>
</td>
</tr>
</asp:ItemTemplate>
<asp:FooterTemplate>
</table></asp:FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>.cs代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
string ConStr = (string)ConfigurationManager.ConnectionStrings["9957_PRODUCT_SMSConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConStr);
SqlCommand com=new SqlCommand("select * from 学生",con);
con.Open();
SqlDataReader dr=com.ExecuteReader();
Repeater1.DataSource = dr;
Repeater1.DataBind();
con.Close();
}
}我的表里有数据的。我调试的dr.Depth=0 dr.FieldCount=13 dr.HasRows=true dr.IsColsed=False dr.RecordsAffected=-1 dr.VisibleFieldCount=13
Repeater1里面相应的属性值也同上,是一样的!加上这句:
if (dr.Read())
{
Response.Write(dr[1]);
}
输出值正确啊
?
.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<asp:HeaderTemplate>
<table>
<tr>
<td>
编号</td>
<td>
身份证号码</td>
<td>
姓名</td>
</tr>
</asp:HeaderTemplate>
<asp:ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="bianhao" Text='<%# Eval("员工编号") %>'/>
</td>
<td>
<asp:Label runat="server" ID="shenfenzheng" Text='<%# Eval("身份证号码") %>'/>
</td>
<td>
<asp:Label runat="server" ID="xingming" Text='<%# Eval("学生姓名") %>'/>
</td>
</tr>
</asp:ItemTemplate>
<asp:FooterTemplate>
</table></asp:FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>.cs代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
string ConStr = (string)ConfigurationManager.ConnectionStrings["9957_PRODUCT_SMSConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConStr);
SqlCommand com=new SqlCommand("select * from 学生",con);
con.Open();
SqlDataReader dr=com.ExecuteReader();
Repeater1.DataSource = dr;
Repeater1.DataBind();
con.Close();
}
}我的表里有数据的。我调试的dr.Depth=0 dr.FieldCount=13 dr.HasRows=true dr.IsColsed=False dr.RecordsAffected=-1 dr.VisibleFieldCount=13
Repeater1里面相应的属性值也同上,是一样的!加上这句:
if (dr.Read())
{
Response.Write(dr[1]);
}
输出值正确啊
?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货