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

后端 未结 9 1538
心在旅途
心在旅途 2020-12-03 09:26

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?

相关标签:
9条回答
  • 2020-12-03 10:09

    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";
    
    0 讨论(0)
  • 2020-12-03 10:21

    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.

    0 讨论(0)
  • 2020-12-03 10:23

    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.

    0 讨论(0)
提交回复
热议问题