1

In other words, let's say I have a bunch of divs and they're all nested under one div.

Can I do something on the outer div so that I don't have to write runat="server" on each of the inner divs, but still access them by id from my code-behind?

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • You could write javascript. In my experience, *most* of the time I want to expose a `div` to my code behind, I'm just putting off writing javascript to do the work. :-) – drdwilcox Dec 21 '11 at 01:19
  • possible duplicate of [Why does ASP.NET Webforms use the "Runat=Server" attribute?](http://stackoverflow.com/questions/304290/why-does-asp-net-webforms-use-the-runat-server-attribute) – George Stocker Dec 21 '11 at 18:19

2 Answers2

1

Sorry to confirm your findings, but all .NET controls accessed in the code-behind must have runat="server" and parent controls don't cascade them down like ViewState settings do.

Why does ASP.NET Webforms use the “Runat=Server” attribute?

Community
  • 1
  • 1
rick schott
  • 21,012
  • 5
  • 52
  • 81
1

Yeah, it's a problem. But if you want just to do something not too complex with inner divs, for example to change the color or something like that you could try to change the InnerHtml property:

<div id="divTest" runat="server">
        <div id="test1" style="color:black">test1</div>
        <div id="test2" style="color:black">test2</div>
    </div>



protected void Page_Load(object sender, EventArgs e)
        {
            divTest.InnerHtml = divTest.InnerHtml.Replace("black", "blue");
        }

The other way is to use jQuery to get every control.

Kath
  • 1,834
  • 15
  • 17