Repeater多个绑定问题

ChinaXtHuLang 2010-12-06 03:51:50
这是我的后台代码:
public partial class List : System.Web.UI.Page
{
public string strPageTitle;
ClassDB DB = new ClassDB();
protected void Page_Load(object sender, EventArgs e)
{
strPageTitle = "数据列表";
if (!IsPostBack)
{
ArtList(1);
}
}
/// <summary>
/// 读取文章列表
/// </summary>
/// <param name="sType">文章类型</param>
public void ArtList(int sType)
{
SqlConnection myConn = DB.GetConnection();//得到数据库连接
string strSql = "Select ID,oTitle,oNow From News Where 1 = 1";
if (sType != null && sType != 0)
{
strSql += " And oBid = " + sType + " ";
}
strSql += " Order By Id Desc";
SqlCommand oIsCmd = new SqlCommand(strSql, myConn);
myConn.Open();
RepeaterList.DataSource = oIsCmd.ExecuteReader();
RepeaterList.DataBind();
myConn.Close();
}
}


这是前台代码:
<asp:Repeater ID="RepeaterList" runat="server">
<HeaderTemplate>
<div class="Box">
<h5>
Asp编程</h5>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><span>
<%#Eval("oNow")%></span><a href="View.aspx?id=<%#Eval("id")%>" target="_blank"><%#Eval("oTitle")%></a></li>
</ItemTemplate>
<FooterTemplate>
</ul> </div>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<div class="Box">
<h5>
Asp.Net编程</h5>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><span>
<%#Eval("oNow")%></span><a href="View.aspx?id=<%#Eval("id")%>" target="_blank"><%#Eval("oTitle")%></a></li>
</ItemTemplate>
<FooterTemplate>
</ul> </div>
</FooterTemplate>
</asp:Repeater>


现在我的那个ArtList方法绑定的是:RepeaterList
我的ArtList方法是根据分类调用的。。不知道如何绑定多个Repeater,我不想多写几个绑定的方法。
我想直接这样调用:ArtList(2);来显示
...全文
335 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
#16

protected void Page_Load(object sender, EventArgs e)
{
strPageTitle = "数据列表";
if (!IsPostBack)
{
ArtList(1, this.RepeaterList);
ArtList(2, this.Repeater1);
}
}
孟子E章 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 chinaxthulang 的回复:]

引用 17 楼 net_lover 的回复:

ArtList(1, (Repeater)this.form1.FindControl("RepeaterList"));

呵呵。可以了。但是我的那个页面没有用:
<form id="form1" runat="server"></form>
现在加上了。就可以了。
问下。如果没有:<form id="form1" runat="s……
[/Quote]
那就是使用RepeaterList的父控件的id,具体是什么可以自己决定啊。如直jie Page.FindControl或者PanelID.FindControl等等等

搞清楚原理,可以举一反三的
ChinaXtHuLang 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 net_lover 的回复:]

ArtList(1, (Repeater)this.form1.FindControl("RepeaterList"));
[/Quote]
呵呵。可以了。但是我的那个页面没有用:
<form id="form1" runat="server"></form>
现在加上了。就可以了。
问下。如果没有:<form id="form1" runat="server"></form>。应该怎么调用啊?
孟子E章 2010-12-07
  • 打赏
  • 举报
回复
ArtList(1, (Repeater)this.form1.FindControl("RepeaterList"));
ChinaXtHuLang 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wuyq11 的回复:]

public void ArtList(int sType,Repeater rpt)
{
SqlConnection myConn = DB.GetConnection();
string strSql = "Select ID,oTitle,oNow From News Where 1 = 1";
if (sType != nu……
[/Quote]

我这样调用好像不行:
protected void Page_Load(object sender, EventArgs e)
{
strPageTitle = "数据列表";
if (!IsPostBack)
{
ArtList(1, "RepeaterList");
ArtList(2, "Repeater1");
}
}


错误 2 参数 2: 无法从“string”转换为“System.Web.UI.WebControls.Repeater”

这是我的命名空间:
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.SqlClient;
using System.Web.UI.HtmlControls;
using System.Configuration;

h121327583 2010-12-06
  • 打赏
  • 举报
回复
如同仁堂
begintransaction 2010-12-06
  • 打赏
  • 举报
回复
我做过一个集合通过数据库一个设定字段进行分解成多个集合然后根据数据库设定的这个字段进行对号加载Reapter的
begintransaction 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]
int x = 2;
Repeater Repeaterx = this.form1.FindControl("Repeater" + x) as Repeater;
Repeaterx.DataSource = ArtList(x);
Repeaterx.DataBind();
这样是可以的啊
[/Quote]
好主意,学习了
在路上20130607 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wuyq11 的回复:]

public void ArtList(int sType,Repeater rpt)
{
SqlConnection myConn = DB.GetConnection();
string strSql = "Select ID,oTitle,oNow From News Where 1 = 1";
if (sType != nu……
[/Quote]

此人有被难住的问题吗
wuyq11 2010-12-06
  • 打赏
  • 举报
回复
public void ArtList(int sType,Repeater rpt)
{
SqlConnection myConn = DB.GetConnection();
string strSql = "Select ID,oTitle,oNow From News Where 1 = 1";
if (sType != null && sType != 0)
{
strSql += " And oBid = " + sType + " ";
}
strSql += " Order By Id Desc";
SqlCommand oIsCmd = new SqlCommand(strSql, myConn);
myConn.Open();
rpt.DataSource = oIsCmd.ExecuteReader();
rpt.DataBind();
myConn.Close();
}
或定义Repeater rpt 为类属性
findcontrol查询控件repeater,传递到ArtList
ChinaXtHuLang 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zx75991 的回复:]

引用 7 楼 chinaxthulang 的回复:
2为大侠人呢???

你想要达到的效果是什么样的?
为什么限定只写一个方法?
[/Quote]
方便调用啊。。
天下在我心 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chinaxthulang 的回复:]
2为大侠人呢???
[/Quote]
你想要达到的效果是什么样的?
为什么限定只写一个方法?
ChinaXtHuLang 2010-12-06
  • 打赏
  • 举报
回复
2为大侠人呢???
ChinaXtHuLang 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zx75991 的回复:]
绑定多个当然可以,带入不同的参数就行了。关键的是你绑定的字段要和查询出来的字段相符合。
如果涉及不同表不同字段的绑定,可能楼主还要做一些改进才行。
[/Quote]
如何改进啊???
天下在我心 2010-12-06
  • 打赏
  • 举报
回复
绑定多个当然可以,带入不同的参数就行了。关键的是你绑定的字段要和查询出来的字段相符合。
如果涉及不同表不同字段的绑定,可能楼主还要做一些改进才行。
ChinaXtHuLang 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 net_lover 的回复:]
另外也可以动态创建Repeater 绑定,有几个分类,就可以new 几个Repeater
[/Quote]
嘿嘿。。听起来很好耶。怎么写啊?
ChinaXtHuLang 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]
int x = 2;
Repeater Repeaterx = this.form1.FindControl("Repeater" + x) as Repeater;
Repeaterx.DataSource = ArtList(x);
Repeaterx.DataBind();
这样是可以的啊
[/Quote]

老大啊。我初学.NET。不知道这个写在哪里啊。哎。。我就学过ASP
孟子E章 2010-12-06
  • 打赏
  • 举报
回复
另外也可以动态创建Repeater 绑定,有几个分类,就可以new 几个Repeater
孟子E章 2010-12-06
  • 打赏
  • 举报
回复

int x = 2;
Repeater Repeaterx = this.form1.FindControl("Repeater" + x) as Repeater;
Repeaterx.DataSource = ArtList(x);
Repeaterx.DataBind();
这样是可以的啊


62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧