我有一个方法找到所有控件,遍历它们,确定它们是文本框,下拉列表等等.检索它们的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;
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有使用 Lambda 表达式,而是创建了一个为我处理控件的方法,并且根据控件的名称,它将该部分设置为 true
public bool setGroup(Control ctrl)
{
isAControl = false;
//set a section to true, so it will pull the html
if (ctrl.ID.StartsWith("GenInfo_"))
{
GenInfo = true;
lstControls.Add(ctrl.ID.Replace("GenInfo_", ""));
isAControl = true;
return isAControl;
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码的一小段,我只想检查某些控件(以加快速度),并且我浏览每个控件,因为每个控件都有不同的方式来获取值(文本框将使用 .text,而 dropdownlist 将使用 .text)。选定的值)
if(control is TextBox || control is DropDownList || control is RadioButton || control is RadioButtonList
|| control is CheckBox || control is CheckBoxList)
{
if (control is TextBox)
{
TextBox txt = control as TextBox;
if (txt.Text != "" && txt.Text != "YYYY/MM/DD")
{
setGroup(control);
if (isAControl)
{
string controlNoGroup = lstControls.Last();
strHtml = strHtml.Replace("@" + (controlNoGroup.ToString()) + "@", txt.Text);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15371 次 |
最近记录: |