Repeater控件为什么ItemTemplate显示错了一行?
前台aspx文件:<formid="form1"runat="server"><div><asp:RepeaterID="Repeater1"runat="server"...
前台aspx文件:
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server"
onitemcreated="Repeater1_ItemCreated" onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("id") %></td>
<td><%# Eval("name") %></td>
<td><%# Eval("age") %></td>
<td><%# aa %></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
后台aspx.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
public string aa = "aaaaaa";
int a = 0;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand comm = new SqlCommand();
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=880516;database=lianxi2");
SqlDataReader dr;
dt = new DataTable();
comm.Connection = conn;
comm.CommandText = "select * from student";
comm.CommandType = CommandType.Text;
if (conn.State.ToString() == "Closed")
conn.Open();
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
comm.Dispose();
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)//Repeater中的所有的数据行
{
if (dt.Rows[a][1].ToString() == "李四")
{
aa = "李四";
a++;
}
else if (dt.Rows[a][1].ToString() == "张三")
{
aa = "张三";
a++;
}
else if (dt.Rows[a][1].ToString() == "王静")
{
aa = "王静";
a++;
}
}
}
运行之后,显示的结果:
哪位大侠能够解决我的问题,另外加30分财富值!! 展开
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server"
onitemcreated="Repeater1_ItemCreated" onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("id") %></td>
<td><%# Eval("name") %></td>
<td><%# Eval("age") %></td>
<td><%# aa %></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
后台aspx.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
public string aa = "aaaaaa";
int a = 0;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand comm = new SqlCommand();
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=880516;database=lianxi2");
SqlDataReader dr;
dt = new DataTable();
comm.Connection = conn;
comm.CommandText = "select * from student";
comm.CommandType = CommandType.Text;
if (conn.State.ToString() == "Closed")
conn.Open();
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(dr);
comm.Dispose();
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)//Repeater中的所有的数据行
{
if (dt.Rows[a][1].ToString() == "李四")
{
aa = "李四";
a++;
}
else if (dt.Rows[a][1].ToString() == "张三")
{
aa = "张三";
a++;
}
else if (dt.Rows[a][1].ToString() == "王静")
{
aa = "王静";
a++;
}
}
}
运行之后,显示的结果:
哪位大侠能够解决我的问题,另外加30分财富值!! 展开
3个回答
展开全部
你好!
目测好像问题有点纠结,不是很好改!所以只能推荐你改改代码,实现一样的功能了:
前台的绑定位置
<td><%# Eval("id") %></td>
<td runat="server" id="td1"><%# Eval("name") %></td>
<td><%# Eval("age") %></td>
<td runat="server" id="td2"></td>
后台的绑定事件
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlTableCell td1 = e.Item.FindControl("td1") as HtmlTableCell;
HtmlTableCell td2 = e.Item.FindControl("td2") as HtmlTableCell;
if (td1.InnerText == "李四")
{
td2.InnerText = "李四";
}
else if (td1.InnerText == "张三")
{
td2.InnerText = "张三";
}
else if (td1.InnerText == "王静")
{
td2.InnerText = "王静";
}
}
}
这样就可以达到你要的效果的了!
目测好像问题有点纠结,不是很好改!所以只能推荐你改改代码,实现一样的功能了:
前台的绑定位置
<td><%# Eval("id") %></td>
<td runat="server" id="td1"><%# Eval("name") %></td>
<td><%# Eval("age") %></td>
<td runat="server" id="td2"></td>
后台的绑定事件
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlTableCell td1 = e.Item.FindControl("td1") as HtmlTableCell;
HtmlTableCell td2 = e.Item.FindControl("td2") as HtmlTableCell;
if (td1.InnerText == "李四")
{
td2.InnerText = "李四";
}
else if (td1.InnerText == "张三")
{
td2.InnerText = "张三";
}
else if (td1.InnerText == "王静")
{
td2.InnerText = "王静";
}
}
}
这样就可以达到你要的效果的了!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呃,刚才也是回答你的问题吧?我经常用的是DataReader:
Repeater1.DataSource =dr;
Repeater1.DataSource =dr;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询