开发者

finding all the HtmlInputCheckBox controls with master.pages with recursion

开发者 https://www.devze.com 2023-03-11 17:25 出处:网络
I thought this would work. But it\'s not wor开发者_如何学Cking.It\'s never getting inside of if (c is HtmlInputCheckBox)

I thought this would work. But it's not wor开发者_如何学Cking. It's never getting inside of if (c is HtmlInputCheckBox)

private string GetAllCheckBoxes(ControlCollection controls)
{
    StringBuilder sb = new StringBuilder();

    foreach (Control c in controls)
    {
        if (c.HasControls())
        {
            GetAllCheckBoxes(c.Controls);
        }
        else
        {
            if (c is HtmlInputCheckBox)
            {
                CheckBox cb = c as CheckBox;
                if (cb.Checked)
                {                                
                    sb.Append(cb.ID + "_1");                                
                }
                else
                {
                    sb.Append(cb.ID + "_0");
                }
            }
        }
    }
    return sb.ToString();

}

update: c is throwing somesort of error.

Parent = {InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(((System.Web.UI.HtmlControls.HtmlGenericControl)(c.Parent)))).InnerText' threw an exception of type 'System.Web.HttpException'}


HtmlInputCheckBox and CheckBox are different classes and do not inherit from the other. An is test on one won't work for the other and vice versa. Sounds like the control is probably an instance of a CheckBox so change the conditional to:

if (c is CheckBox)

0

精彩评论

暂无评论...
验证码 换一张
取 消