我有一个方法找到所有控件,遍历它们,确定它们是文本框,下拉列表等等.检索它们的ID名称,并根据ID名称它将设置一个布尔语句(因此我会知道如果表格的那一部分是完整的,并将通过电子邮件发送给某一群人)不幸的是,这是通过太多的if语句完成的,并且想知道我是否可以得到一些帮助使这个更易于管理
protected void getEmailGroup() { Control[] allControls = FlattenHierachy(Page); foreach (Control control in allControls) { if (control.ID != null) { if (control is TextBox) { TextBox txt = control as TextBox; if (txt.Text != "") { if (control.ID.StartsWith("GenInfo_")) { GenInfo = true; } if (control.ID.StartsWith("EmpInfo_")) { EmpInfo = true; } } } if (control is DropDownList) { DropDownList lb = control as DropDownList; if (lb.Selectedindex != -1) { if (control.ID.StartsWith("GenInfo_")) { GenInfo = true; } if (control.ID.StartsWith("EmpInfo_")) { EmpInfo = true; } } } } } }
解决方法
为什么不使用Control.FindControl(string)方法?
来自:http://msdn.microsoft.com/en-us/library/486wc64h.aspx
private void Button1_Click(object sender,EventArgs MyEventArgs) { // Find control on page. Control myControl1 = FindControl("TextBox2"); if(myControl1!=null) { // Get control's parent. Control myControl2 = myControl1.Parent; Response.Write("Parent of the text Box is : " + myControl2.ID); } else { Response.Write("Control not found"); } }
原文地址:https://www.jb51.cc/csharp/99319.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。