如何在 .NET 中使用 C# 编辑 div 的 CSS 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/175381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:41:49  来源:igfitidea点击:

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

c#.netcss

提问by Jon Smock

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?

我试图在后面的代码(C#)中获取一个 div 的 ID 并在其上设置一些 css。我可以从 DOM 中获取它还是必须使用某种控件?

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

采纳答案by Rob

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

runat="server"向其添加属性,以便您拥有:

<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:Panelcontrol is virtually synonymous (at least as far as rendered markup is concerned) with div, so you could also do:

还值得一提的是,该asp:Panel控件实际上与 同义(至少就呈现的标记而言)div,因此您也可以这样做:

<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.

这使您可以更明确地访问该属性,并且其他属性可能对您有用,也可能没有用。

回答by Jeremy Frey

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

将 runat="server" 属性添加到标记,然后您可以从代码隐藏中引用它。

回答by tvanfosson

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",然后只需在代码隐藏中引用它并设置“class”属性。

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

Code-behind

代码隐藏

formSpinner.Attributes["class"] = "class-name";

回答by StingyHyman

Add runat to the element in the markup

将 runat 添加到标记中的元素

<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.

然后你可以通过使用 formSpinner.Attributes("class") 来获取控件的类属性,它只会是一个字符串,但你应该能够编辑它。

回答by Joel Coehoorn

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.

这个问题让我很紧张。这表明您可能不了解使用服务器端代码将如何影响您页面的 DOM 状态。

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.
  • 表单从客户端提交到 Web 服务器。这是 Web 浏览器可以采取的最慢的操作,尤其是在 ASP.Net 中,表单可能会填充额外的字段(即:ViewState)。过于频繁地为琐碎的活动做这件事会使您的应用程序看起来很迟钝,即使其他一切都很好而且很活泼。
  • 它会在带宽(上行和下行)和 CPU/内存方面为您的服务器增加负载。重建页面所涉及的一切都将不得不再次发生。如果页面上有动态控件,不要忘记创建它们。
  • 自上次请求以来您对 DOM 所做的任何事情都将丢失,除非您记得为此请求再次执行此操作。您页面的 DOM 已重置

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.

如果你可以逃脱它,你可能想把它推到 javascript 并避免回发。也许使用 XmlHttpRequest() 调用来触发您需要的任何服务器端操作。

回答by Joel Coehoorn

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.

如果您想要做的只是有条件地显示或隐藏 <div>,那么您可以将其声明为 <asp:panel>(呈现为 html 作为 div 标记)并设置它的 .Visible 属性。

回答by mokumaxCraig

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

如果没有 runat="server",你如何做到这一点?例如,如果您有一个

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

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

...并尝试从Updatepanel 中更新它,它永远不会更新。

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

但是,如果您将其保留为普通的非服务器 HTML 控件,则可以。这是更新它的Jquery:

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

How do you do this in codebehind though?

你如何在代码隐藏中做到这一点?

回答by Peri

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,

如果你不想让你的控件运行服务器以防你需要 ID 或者只是不想将它添加到视图状态,

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

in the back-end:

在后端:

protected string _css = "modalBackground";

回答by RandomUs1r

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

要扩展 Peri 的帖子以及为什么我们可能不想使用 viewstate,请使用以下代码:

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.

style="<%= _myCSS %>"

Protected _myCSS As String = "display: none"
如果您使用 AJAX,这是查看的方法,它允许通过 asp.net 后端代码而不是 jquery/jscript 操作显示。