for循环Label,不知道怎么写了?

love560 2011-01-10 03:14:17
主要 是现在要做个页面,查询里面所有的字段,不想一个个写了,但是刚学c#,又不知道怎么写了?
前台代码:

<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="13px"></asp:Label></td>
<asp:Label ID="Label2" runat="server"></asp:Label>
<asp:Label ID="Label3" runat="server"></asp:Label>
<asp:Label ID="Label4" runat="server"></asp:Label>
<asp:Label ID="Label5" runat="server"></asp:Label>

后台代码:

string id = Request.QueryString["id"].ToString();
OleDbDataReader odr = DB.odr("select * from bus_xinhai where id=" + id + "");
if (odr.Read() == true)
{
for (int i = 1; i < 50; i++)
{
this.Label[i].Text = odr[i].ToString();
//Response.Write(odr[i].ToString()+"<br>");
}
}
odr.Close();

这个 this.Label[i].Text = odr[i].ToString();
这句不知道怎么写了?

...全文
366 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
love560 2011-01-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 chen_ya_ping 的回复:]
你可以保证那个数据库中读取的记录的书跟那个label的数量匹配吗?
[/Quote]

不确定,哎 先结贴,回头慢慢研究吧
chen_ya_ping 2011-01-10
  • 打赏
  • 举报
回复
你可以保证那个数据库中读取的记录的书跟那个label的数量匹配吗?
juxianxiang 2011-01-10
  • 打赏
  • 举报
回复
用findcontrol,(label)this.page.findcontrol(lable)。text
foxkiller 2011-01-10
  • 打赏
  • 举报
回复
关于那个label, ASP.Net里面是
for(int i = 1,i<49,i++){
strLabelName = "label" + i;
Label _label = this.FindControl(strLabelName);
_label.Text = *;
}
\\WinForm把this.FindControl 改成this.Controls [strLabelName] as Label;
yanxiaodi 2011-01-10
  • 打赏
  • 举报
回复
baidu的 仅供参考

TDBOperator db = null;

try

{

db = TDBOperatorFactory.GetDBOperator(comFunction.getDBLink());

//打开数据库连接

db.Open();

System.Data.Common.DbConnection dc = db.getDbConnection();

string[] res = new string[4];

res[1] = "dbo";

//取得全部表名

DataTable dt = dc.GetSchema("Tables");

this.Text = dt.Rows.Count.ToString();

dt.WriteXml("table.xml");

foreach (System.Data.DataRow row in dt.Rows)

{

//取出数据库中系统表外的所有表名

if (row[3].ToString() == "TABLE")

{

listBox1.Items.Add(row[2].ToString());

listBox1.Items.Add("============================");

for (int i = 0; i < 4; i++) res[i] = null;

res[2] = row[2].ToString();

//取得相应表的所有字段

DataTable dtColumns = dc.GetSchema("Columns", res);

foreach (System.Data.DataRow rowColumns in dtColumns.Rows)

{

listBox1.Items.Add(rowColumns[3].ToString());

}

dtColumns.WriteXml(row[2].ToString() + "b.xml");

listBox1.Items.Add("============================");

}

}

}

catch (Exception ex)

{

comMsgBox.showError(ex.Message);

}

finally

{

db.Close();

}

yanxiaodi 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 love560 的回复:]
引用 9 楼 love560 的回复:
引用 5 楼 wuyq11 的回复:
还有个小问题,就是 .net 怎么读取表中的字段的名字呀? 不是表里的数据,而是字段的名字呀?
……


我用的是access数据库 OleDb 的,求高人指点!
[/Quote]
.net自带的SDK里有详细的教程
luyuwei2008 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fengyarongaa 的回复:]
C# code

1.foreach (Control c in this.Controls[1].Controls)
{
if (c is Label)
{
Label lbl = (Label)c;
}
}

2.private void GetLabel(Control c)
{
if……
[/Quote]
正解!!
TimZhuFaith 2011-01-10
  • 打赏
  • 举报
回复
哈哈。。。说lz的[Quote=引用 4 楼 fengyarongaa 的回复:]
引用 3 楼 timzhufaith 的回复:

for (int i = 1; i < 50; i++)
为什么加这句?


???? 我加了么
[/Quote]
love560 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 love560 的回复:]
引用 5 楼 wuyq11 的回复:
还有个小问题,就是 .net 怎么读取表中的字段的名字呀? 不是表里的数据,而是字段的名字呀?
……
[/Quote]

我用的是access数据库 OleDb 的,求高人指点!
love560 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
if (odr.Read() == true)
{
for (int i = 1; i < 50; i++)
{
((Label)this.FindControl("label"+i)).Text = odr[i].ToString();
Response.Write(odr[i].ToString()+"<br>");
}
}
[/Quote]

谢谢哈,我用这个了,简单点,刚学,让大家见笑了哈,还有个小问题,就是 .net 怎么读取表中的字段的名字呀? 不是表里的数据,而是字段的名字呀?

wangchangming 2011-01-10
  • 打赏
  • 举报
回复
for(int i-0;i<50;i++)
{
label lb=FindControl("Lable"+i) as Label;
if(lb != null)
{
lb.text=i;
}
}
fox_sky 2011-01-10
  • 打赏
  • 举报
回复
int[] arr = new int[] { 1, 2, 3, 4, 5 };//模拟数据源
for(int i=1;i<50;i++)
{
Control control = this.FindControl("Label" + i);//这里找Label
if (control != null)
{
Label lal = control as Label;
if (lal != null)
{
lal.Text = i.ToString();
}
}
}
laowang134 2011-01-10
  • 打赏
  • 举报
回复
前台有49个label控件
wuyq11 2011-01-10
  • 打赏
  • 举报
回复
string id = Request.QueryString["id"].ToString();
OleDbDataReader odr = DB.odr("select * from bus_xinhai where id=" + id + "");
if (odr.Read() == true)
{
for (int i = 1; i < 50; i++)
{
((Label)this.FindControl("label"+i)).Text = odr[i].ToString();
}
}
odr.Close();

ycproc 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 timzhufaith 的回复:]

for (int i = 1; i < 50; i++)
为什么加这句?
[/Quote]

???? 我加了么
TimZhuFaith 2011-01-10
  • 打赏
  • 举报
回复
for (int i = 1; i < 50; i++)
为什么加这句?
ycproc 2011-01-10
  • 打赏
  • 举报
回复

1.foreach (Control c in this.Controls[1].Controls)
{
if (c is Label)
{
Label lbl = (Label)c;
}
}

2.private void GetLabel(Control c)
{
if (c.GetType().Name == "Label")
{

}
else if (c.HasControls())
{
for (int i = 0; i < c.Controls.Count; i++)
{
GetLabel(c.Controls[i]);
}
}
}

love560 2011-01-10
  • 打赏
  • 举报
回复
还有 就是不知道怎么处理这个东西更好些?

62,046

社区成员

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

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

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

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