How to edit CSS-style div using C # in .NET.

I am trying to grab the div id in the code behind (C #) and install some css on it. Can I take it from the DOM or do I need to use some kind of control?

<div id="formSpinner"> <img src="images/spinner.gif" /> <p>Saving...</p> </div> 
+46
c # css
Oct 06 '08 at 17:46
source share
9 answers

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

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

This way you can access the class attribute using:

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

It's also worth mentioning that the asp:Panel control is actually a synonym (at least in terms of markup) with a div , so you can also do:

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

Which then allows you to write:

 formSpinner.CssClass = "classOfYourChoice"; 

This gives you more specific access to the property, and there are others that may or may not be useful to you.

+74
06 Oct '08 at 18:07
source share

Make sure your div is set to runat = "server" and then just reference it in the code and set the class attribute.

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

Code for

 formSpinner.Attributes["class"] = "class-name"; 
+13
Oct 06 '08 at 17:51
source share

This question makes me nervous. This indicates that you may not understand how server-side code usage will affect the state of the DOM page.

Whenever you run the code on the server side, the whole page is rebuilt from scratch. This has several consequences:

  • The form is sent from the client to the web server. This applies to the slowest actions that a web browser can perform, especially in ASP.Net, where the form can be supplemented with additional fields (i.e.: ViewState). Doing this too often for trivial actions will make your application look sluggish, even if everything else is enjoyable and fast.
  • It adds load to your server in terms of bandwidth (upstream and downstream) and processor / memory. All that is required to restore your page should be repeated. If there are dynamic controls on the page, be sure to create them.
  • All that you have done with the DOM, since the last request is lost, if you do not forget to do it again for this request. Your DOM page is reset.

If you succeed, you can click this on javascript and avoid postback. Perhaps use the XmlHttpRequest () call to trigger any server-side action you need.

+6
Oct 06 '08 at 18:29
source share

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

+3
Oct 06 '08 at 17:50
source share

Add runat to the markup element

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

Then you can go to the attributes of the management class using formSpinner.Attributes ("class") This will only be a string, but you can edit it.

+3
Oct 06 '08 at 17:51
source share

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

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

... and try updating it from Updatepanel , it will never be updated.

However, if you save it as regular non-server HTML control, you can. Here jQuery updates it:

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

How do you do this in Codebehind?

+3
Feb 14 2018-11-22T00:
source share

If you do not want to create your own runat server in case you need an identifier or just do not want to add it to the viewstate,

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

in the background:

 protected string _css = "modalBackground"; 
+3
Feb 27 '11 at 3:24
source share

If all you want to do is conditionally show or hide the <div>, then you can declare it as <asp: panel> (displays html as a div tag) and set it. Vibible Property.

+1
Oct 06 '08 at 21:26
source share

To go to the Peri post and why we cannot use viewstate in the following code:

style="<%= _myCSS %>"

Protected _myCSS As String = "display: none"
Whether the approach is suitable for use if you use AJAX, it allows you to manipulate the display using asp.net back-end code, rather than jquery / jscript.

+1
Jan 15 '13 at 20:08
source share



All Articles