请大家来帮我看看,C#中如何获取一个父控件上的指定子控件? 我在一个panel控件中有很多控件,如:DataGridView,textBox,Button等。现我想得到Button控件,请问要怎么办啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Button btn=this.panel1..FindControl("Button的ID") as Button;if(btn!=null){ //to do} Button btn=this.panel1.FindControl("Button的ID") as Button; if(btn!=null) { //to do } foreach(control in panel1.controls){ if(control is button) //handle something....} 意思是现在我在运行中,不知道Button的ID,能不能通过一个循环来找到呢? panel1.Controls.Add(new button("btnP")); foreach(Control c in this.panel1.Controls){if(c is Button){}} foreach(Control c in this.panel1.Controls) { if(c is Button) { } } 给你一个参考:#region 清空文本框的值 protected void ClearText(Control control) { foreach (Control ct in control.Controls) { if (ct is TextBox) { if (ct.ID != "tbpnr") { ((TextBox)ct).Text = ""; } } else if (ct.HasControls()) { ClearText(ct); } } } #endregion 我来纠正你们一个错误哦winform 里面 找控件 用Controls.find()例如:Button btn = this.panel1.Controls.Find("控件名")[0] as Button;//注意,找到的是个数组,所以要有索引。ASP.NET里面 找控件 用Control.FindControl("控件名")例如:Button btn = this.panel1.FindControl("控件名") as Button;//直接找到一个控件,不是数组哦 关于此系统的开发 .NET MVC求助 C#按钮快捷键问题 [求助]未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 如何使用画刷编辑一个bmp图? 如何监视并控制AxWebBrowser下载网页中的图片 看下2段代码的区别在哪? 问个关于panel的问题,很基本,,但是困扰了我很长时间。。。。。60分送上 创建Web应用程序时,现在却提示说Web服务运行的不是Asp.Net1.1版本. 关于System.Web.Mail问题 求救... C#问题,如下
if(btn!=null)
{
//to do
}
if(btn!=null)
{
//to do
}
{
if(control is button)
//handle something....
}
{
if(c is Button)
{
}
}
{
if(c is Button)
{
}
}
#region 清空文本框的值
protected void ClearText(Control control)
{
foreach (Control ct in control.Controls)
{
if (ct is TextBox)
{
if (ct.ID != "tbpnr")
{
((TextBox)ct).Text = "";
}
}
else if (ct.HasControls())
{
ClearText(ct);
} }
}
#endregion
例如:
Button btn = this.panel1.Controls.Find("控件名")[0] as Button;//注意,找到的是个数组,所以要有索引。
ASP.NET里面 找控件 用Control.FindControl("控件名")
例如:
Button btn = this.panel1.FindControl("控件名") as Button;//直接找到一个控件,不是数组哦