大家进来研究,有关is的问题?

jerrie_1 2005-09-30 10:11:47
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox1;

private void Page_Load(object sender, System.EventArgs e)
{
foreach(System.Web .UI .Control ct in this.Controls )
{
TextBox tb;
if (ct is TextBox)
{
tb=(TextBox)ct;
Label1.Text =tb.Text+" " ;
}
}
}

}
问题:为何Label1.Text总是为空呢?跟踪了一下,其中if (ct is TextBox)中总是为false;调试无错误。
...全文
96 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
appleseeker 2005-09-30
  • 打赏
  • 举报
回复
yangxd_yi(杨一) , julong88(村长说了“A little knowledge is a dangerous thing ”。所以俺) 都对的。
我测试过了。
jerrie_1 2005-09-30
  • 打赏
  • 举报
回复
不好意思!忘了表达清楚啦!
jerrie_1 2005-09-30
  • 打赏
  • 举报
回复
TO:cj460cn(cj460cn)
先谢谢!但你的有错误,类型转化错误!


TO:julong88(村长说了“A little knowledge is a dangerous thing ”。所以俺)
谢谢!想问一下,你是在WIN下面调试的还是在WEB下面呢?我要的是WEB
yangxd_yi 2005-09-30
  • 打赏
  • 举报
回复
楼上正解

winform下测试通过:

public void button3_Click(object sender, System.EventArgs e)
{
test();
}
public void test()
{
foreach(Control c in this.Controls)
{
if(c is TextBox)
this.label2.Text +=c.Text+"##########";
}

}
julong88 2005-09-30
  • 打赏
  • 举报
回复
private void showControl()
{
foreach(Control c in this.Controls)
{
if(c is TextBox)
MessageBox.Show(c.Name);
}
}

调试通过
cj460cn 2005-09-30
  • 打赏
  • 举报
回复
foreach(System.Windows.Forms.TextBox test in this.Controls)
{
Label1.Text =test.Text+" " ;
}
这样写的话,就不用加is,也不用强制转换什么的了。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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