-1

I have a div that is runat="server". The div is contained in a panel, and within the div are some controls. All of the controls outside of the div (but within the panel) are cleared when I run my "ClearControlsInPanel()" method, which looks like this:

public static void ClearControlsInPanel(Panel paneltoclear, string[] ignorelist)
{
    foreach (Control c1 in paneltoclear.Controls)
    {
        if (c1 is TextBox)
        {
            if (!ignorelist.Contains(c1.ID.ToString()))
            {
                ((TextBox)c1).Text = "";
            }
        }
        if (c1 is DropDownList)
        {
            if (!ignorelist.Contains(c1.ID.ToString()))
            {
                ((DropDownList)c1).SelectedIndex = 0;
            }
        }
        //etc.
    }
}

Once the div is reached, I cannot see the controls in it, and thus none of those controls get cleared. Ironically I found a guy who made a post about the exact same thing, Why adding runat=server to a div tag throws an exception of type 'System.Web.HttpException in controls collection? But the thread ends, with no real solution or explanation.

The full exception is:

base {System.Web.UI.HtmlControls.HtmlContainerControl} = {InnerText = 
'((System.Web.UI.HtmlControls.HtmlContainerControl)
(((System.Web.UI.HtmlControls.HtmlGenericControl)(paneltoclear.Controls._controls
[165])))).InnerText' threw an exception of type 'System.Web.HttpException'}

Please assume that this must remain a runat server div, and cannot be changed to a panel. (I'm almost certain changing it to a panel will solve it, but we have other requirements that need this to be a runat server div (long story)).

CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87
  • none of the code you have posted shows where your accessing the HtmlGeneticControl, without code its not going to be very helpful. – Lloyd Mar 28 '12 at 03:17
  • @Lloyd That's just it, I'm not accessing any HtmlGenericControl from any code I've written. It fails on the above foreach (Control c1 in paneltoclear.Controls). The only other thing I can post is the actual div itself in the .aspx page,
    – CptSupermrkt Mar 28 '12 at 03:35
  • you have written " Once the div is reached, I cannot see the controls in it, and thus none of those controls get cleared." what code are you using to clear the controls inside the div? – Lloyd Mar 28 '12 at 03:56
  • This method, ClearControlsInPanel. The foreach loop goes through each control in the panel, and sets it to either "", unchecked, selectedindex = 0, etc. Problem is, when it encounters a div control, it throws that exception, and the foreach loop doesn't get to any of the controls in the div. – CptSupermrkt Mar 28 '12 at 04:02
  • Wow, a downvote? For real? I'd love to know why. – CptSupermrkt Mar 29 '12 at 00:08

2 Answers2

1

A panel is rendered into a div anyway, so why the div? I see no requirement for it and it solves this issue rather quickly :)

Adam
  • 16,089
  • 6
  • 66
  • 109
0

Easier if you post the actual code that accesses the div. i.e. which branch of the if statement actually deals with the generic control case. I'm guessing the problem is that you are trying to set the innertext property of the div (as the exception suggests). The trick might be that if the div control has children, then the innertext property doesn't exist, and any text is represented as a literal in the controls array of the div control?

So to clear the child collection try something like... Also probs better if you do else if vs if

    public static void ClearControlsInPanel(ControlCollection controls, string[] ignorelist)
    {
        foreach (Control c1 in controls)
        {
            if (c1 is TextBox)
            {
                if (!ignorelist.Contains(c1.ID.ToString()))
                {
                    ((TextBox)c1).Text = "";
                }
            }
            else if (c1 is DropDownList)
            {
                if (!ignorelist.Contains(c1.ID.ToString()))
                {
                    ((DropDownList)c1).SelectedIndex = 0;
                }
            }
            else
            {
                if (c1.HasControls())
                {
                    ClearControlsInPanel(c1.Controls, ignorelist);
                }
            }
        }
    }
Mike Walsh
  • 198
  • 6
  • Sorry, I don't quite understand, because literally the only code I have is an event handler (ddlist selection changed), and the single and only line in that is ClearControlsInPanel(pnlform, new string[1] { "ddlallemployees" }); The string is unrelated --- removing it gives the same error. Then, as I mentioned in a comment above, the exception happens on the foreach loop, BEFORE any of the ifs are reached. – CptSupermrkt Mar 29 '12 at 00:11
  • So when I change the ddlist selection, the event fires, it executes the single and only line of code (ClearControlsInPanel()), and then the foreach (Control c1 in paneltoclear.Controls) causes the exception. – CptSupermrkt Mar 29 '12 at 00:12
  • Are you sure you are getting a real execution exception here. I've cut and pasted the code and it runs fine. If you look at in VS debugger you will see an exception when the debugger tries to call the inner text property, but as long as your code doesn't call this then it should be fine??? – Mike Walsh Apr 02 '12 at 06:20
  • The program runs as you say, but because of the exception when calling the inner text property, it doesn't see any of the controls inside of that div. It skips over the entire content of the div. – CptSupermrkt Apr 03 '12 at 07:01
  • Like a few people have said you really need to post the code... Now it seems that the problem is you just aren't clearing the child controls...I'll post some code above... – Mike Walsh Apr 04 '12 at 02:47