Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net在窗体中对控件进行迭代_C#_Asp.net_Checkbox_Webforms - Fatal编程技术网

C# asp.net在窗体中对控件进行迭代

C# asp.net在窗体中对控件进行迭代,c#,asp.net,checkbox,webforms,C#,Asp.net,Checkbox,Webforms,我试图遍历控件列表,以确定在ASP.NET网页中是否选中了哪些复选框。如果复选框被勾选,则将添加其文本 然而,这是一份清单;迭代返回错误或未找到复选框类型的控件(AJAX选项卡容器中有10个复选框)。 这是我的密码副本 private void populateServiceList() { foreach (CheckBox checkbox in this.Form1.Controls.OfType<CheckBox>()) {

我试图遍历控件列表,以确定在ASP.NET网页中是否选中了哪些复选框。如果复选框被勾选,则将添加其文本 然而,这是一份清单;迭代返回错误或未找到复选框类型的控件(AJAX选项卡容器中有10个复选框)。 这是我的密码副本

private void populateServiceList()
    {
        foreach (CheckBox checkbox in this.Form1.Controls.OfType<CheckBox>())
        {

        if (checkbox.Checked == true)
        {
            this.services.Add(checkbox.Text);
        }
    }
}
private void populateServiceList()
{
foreach(this.Form1.Controls.OfType()中的复选框)
{
如果(checkbox.Checked==true)
{
this.services.Add(checkbox.Text);
}
}
}

您可能在表单扫描的根目录中没有复选框


通常,我们使用-递归函数扫描容器中的所有元素。

考虑更新AJAX选项卡控件以使用控件


如果这确实不是一个选项,请更改
this.Form1.Controls
以将直接父控件中的控件集合用于复选框,而不是整个表单。

EDIT@6:07 CST:

在populateServiceList()方法中使用此选项:

List temp=new List();
foreach(TabContainer1.Controls中的控件ctrl)
{
if(ctrl.HasControls())
{
foreach(ctrl.Controls中的Control subctrl)
{
复选框TControl=subctrl作为复选框;
if(TControl!=null&&TControl.Checked)
{
临时添加(t控制文本);
}                         
}
}
}

asp.net,而不是windows窗体。他将很难在页面上的任何位置找到System.Windows.Forms.Checkbox。必须考虑到所有控件都没有文本值。它似乎没有标识和选中的复选框。如果使用“as”进行转换,则需要检查值是否为空。您不必再次验证类型。复选框TControl=subctrl作为复选框;如果(TControl!=null&&TControl.Checked){//do thing here}在SS中用代码示例编辑以使用选项卡面板。为我工作过@海瑞,谢谢!this.Form1.Controls引发错误。无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型为“System.Web.UI.WebControls.CheckBox”。@MasterP-您阅读了我的答案还是只是浏览了一下?我不建议删除.OfType()检查
        List<String> temp = new List<string>();

        foreach (Control ctrl in TabContainer1.Controls)
        {
            if (ctrl.HasControls())
            {
                foreach (Control subctrl in ctrl.Controls)
                {
                    CheckBox TControl = subctrl as CheckBox; 
                    if (TControl != null && TControl.Checked) 
                    {
                        temp.Add(TControl.Text);
                    }                         
                }
            }
        }