46

I'm trying to grab a div's ID in the code behind (C#) and set some css on it. Can I grab it from the DOM or do I have to use some kind of control?

<div id="formSpinner">
    <img src="images/spinner.gif" />
    <p>Saving...</p>
</div>
Rob
  • 45,296
  • 24
  • 122
  • 150
Jon Smock
  • 9,451
  • 10
  • 46
  • 49
  • Adding the runat="server" fixed half my issue. I can now refer to it in my code behind. However, I can't seem to set the css class or style. I'm trying to display: block or display: none. – Jon Smock Oct 06 '08 at 18:20
  • Jon - edit your question to add the code you're putting in the codebehind and WHERE you're putting it, or consider opening a new question to deal with the further issue. :) – Rob Oct 06 '08 at 18:26
  • Sidenote to anyone doing this exact example: check out UpdatePanel's and UpdateProgress controls – Jon Smock Nov 08 '08 at 06:23

9 Answers9

76

Add the runat="server" attribute to it so you have:

<div id="formSpinner" runat="server">
    <img src="images/spinner.gif">
    <p>Saving...</p>
</div>

That way you can access the class attribute by using:

formSpinner.Attributes["class"] = "classOfYourChoice";

It's also worth mentioning that the asp:Panel control is virtually synonymous (at least as far as rendered markup is concerned) with div, so you could also do:

<asp:Panel id="formSpinner" runat="server">
    <img src="images/spinner.gif">
    <p>Saving...</p>
</asp:Panel>

Which then enables you to write:

formSpinner.CssClass = "classOfYourChoice";

This gives you more defined access to the property and there are others that may, or may not, be of use to you.

Rob
  • 45,296
  • 24
  • 122
  • 150
13

Make sure that your div is set to runat="server", then simply reference it in the code-behind and set the "class" attribute.

<div runat="server" id="formSpinner">
   ...content...
</div>

Code-behind

formSpinner.Attributes["class"] = "class-name";
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
6

This question makes me nervous. It indicates that maybe you don't understand how using server-side code will impact you're page's DOM state.

Whenever you run server-side code the entire page is rebuilt from scratch. This has several implications:

  • A form is submitted from the client to the web server. This is about the slowest action that a web browser can take, especially in ASP.Net where the form might be padded with extra fields (ie: ViewState). Doing it too often for trivial activities will make your app appear to be sluggish, even if everything else is nice and snappy.
  • It adds load to your server, in terms of bandwidth (up and down stream) and CPU/memory. Everything involved in rebuilding your page will have to happen again. If there are dynamic controls on the page, don't forget to create them.
  • Anything you've done to the DOM since the last request is lost, unless you remember to do it again for this request. Your page's DOM is reset.

If you can get away with it, you might want to push this down to javascript and avoid the postback. Perhaps use an XmlHttpRequest() call to trigger any server-side action you need.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

Add the runat="server" attribute to the tag, then you can reference it from the codebehind.

Jeremy Frey
  • 2,334
  • 2
  • 22
  • 26
3

Add runat to the element in the markup

<div id="formSpinner" runat="server">
    <img src="images/spinner.gif">
    <p>Saving...</p>
</div

Then you can get to the control's class attributes by using formSpinner.Attributes("class") It will only be a string, but you should be able to edit it.

StingyJack
  • 19,041
  • 10
  • 63
  • 122
3

How do you do this without runat="server"? For example, if you have a

<body runat="server" id="body1">

...and try to update it from within an Updatepanel it will never get updated.

However, if you keep it as an ordinary non-server HTML control you can. Here's the Jquery to update it:

$("#body1").addClass('modalBackground');

How do you do this in codebehind though?

mokumaxCraig
  • 413
  • 1
  • 7
  • 15
3

If you do not want to make your control runat server in case you need the ID or simply don't want to add it to the viewstate,

<div id="formSpinner" class="<%= _css %>">
</div>

in the back-end:

protected string _css = "modalBackground";
Rob
  • 45,296
  • 24
  • 122
  • 150
Peri
  • 31
  • 1
1

To expand on Peri's post & why we may not want to use viewstate the following code:

style="<%= _myCSS %>"

Protected _myCSS As String = "display: none"
Is the approach to look at if you're using AJAX, it allows for manipulating the display via asp.net back end code rather than jquery/jscript.

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
1

If all you want to do is conditionally show or hide a <div>, then you could declare it as an <asp:panel > (renders to html as a div tag) and set it's .Visible property.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794