C# 如何在运行 asp.net 页面时以编程方式设置表格背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1646007/
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-06 19:39:29  来源:igfitidea点击:

How do I set a table background programmatically while running an asp.net page?

c#asp.netbackground-color

提问by Theresa

I have an aspx page that has one background color as the default. I need to be able to change it programmatically when a certain option of a radio button is selected. I tried setting the ID field of the table, but I can't seem to access it in my C# code behind file.

我有一个 aspx 页面,默认背景色为一种。I need to be able to change it programmatically when a certain option of a radio button is selected. 我尝试设置表的 ID 字段,但似乎无法在我的 C# 代码隐藏文件中访问它。

My original table is:

我原来的表是:

<table id="tblSheet" runat="server" style="border-color: #FF9900; border-style: solid; 
border-width:  thin; width:100%; background-color: #99ccff;" cellspacing="4" 
cellpadding="1">

I don't see the id in my intellisense, though.

不过,我在智能感知中没有看到 id。

Edit:
Now that I can see my table in my code behind, I'm trying to actually change the background color. Here is the code for my radiobuttonlist:

编辑:
现在我可以在后面的代码中看到我的表格,我正在尝试实际更改背景颜色。这是我的单选按钮列表的代码:

<asp:RadioButtonList ID="rdoStatus" runat="server" AutoPostBack="true"
RepeatDirection="Horizontal" Visible="true"   
OnSelectedIndexChanged="rdoStatus_OnSelectionChanged">
<asp:ListItem Value="181001" Text="Open"/>
<asp:ListItem Value="181002" Text="Closed" />
<asp:ListItem Value="181003" Text="Pending" />
</asp:RadioButtonList>

I'm hitting the breakpoint I set in the event handler, but the background color is not changing on postback. If the item chosen is Pending, then I want to change the background color to something different. If they change the radio button to Open Or Closed, then I want to make sure the background color is the default.

我遇到了在事件处理程序中设置的断点,但回发时背景颜色没有改变。如果选择的项目是待定的,那么我想将背景颜色更改为不同的颜色。如果他们将单选按钮更改为打开或关闭,那么我想确保背景颜色是默认值。

Edit 2:
The code in my event handler is very simple:

编辑 2:
我的事件处理程序中的代码非常简单:

if (rdoStatus.SelectedValue == "181003")
{
  tblSheet.BgColor = "#ff9a9a";
}
else
{
  tblSheet.BgColor = "#99ccff";
}

采纳答案by Theresa

I got it working!
I changed the table to the following (I removed the background-color):

我让它工作了!
我将表格更改为以下内容(我删除了背景颜色):

<table id="tblSheet" runat="server" style="border-color: #FF9900; border-style: solid; 
border-width: thin; width:100%;" cellspacing="4" cellpadding="1">

Then in my code behind I set the background color in the Page_Load when it's nota postback:

然后在我后面的代码中,当它不是回发时,我在 Page_Load 中设置了背景颜色:

tblSheet.Bgcolor = "#99ccff";

Voila! The radiobuttonlist and event handler are the same as in the question. The postback was changing the color back to the original. Thanks for everyone's help!

瞧!radiobuttonlist 和事件处理程序与问题中的相同。回发正在将颜色改回原始颜色。感谢大家的帮助!

回答by NakedBrunch

Place runat="server"in the table tag

放置runat="server"在表格标签中

Once you've done that you'll be able to access the table programmatically.

完成后,您将能够以编程方式访问该表。

To change the background color directly, try:

要直接更改背景颜色,请尝试:

if (rdoStatus.SelectedValue == "181003")
    {
      tblSheet.Style.Add("background-color", "#ff9a9a");
    }
    else
    {
      tblSheet.Style.Add("background-color", "#99ccff");
    }

if you're using stylesheets you can, try this:

如果您可以使用样式表,请尝试以下操作:

if (rdoStatus.SelectedValue == "181003")
{
  tblSheet.CssClass = "default_color"
}
else
{
  tblSheet.CssClass = "other color"
}

回答by eugeneK

First of all you cannot see the Table because there is no runat="server" attribute of table which allows you to see ID, second of all you won't be able to change a colour of the table even if you see the Table in your code. Even if you will access table's attributes and set a colour there it won't help you either because you will need to post back again to see the change.

首先,您无法看到表格,因为表格没有 runat="server" 属性可以让您看到 ID,其次,即使您看到表格,您也无法更改表格的颜色你的代码。即使您将访问表的属性并在那里设置颜色,它也无济于事,因为您需要再次回发以查看更改。

What you need to do is to use Update panel and use a variable instead of #FF9900, this way default colour will be Red or whatever colour it is and on check event of checkbox you will change the value to Green or other colour.

您需要做的是使用更新面板并使用变量而不是#FF9900,这样默认颜色将是红色或任何颜色,并且在复选框的检查事件中,您将值更改为绿色或其他颜色。

回答by Joel Coehoorn

Are you triggering the postback that will make this change directly from the radio button?
Is this the only thing that changes at this time?

您是否正在触发将直接从单选按钮进行此更改的回发?
这是此时唯一发生变化的事情吗?

If the answer to both of those is "yes", you should consider doing this all in javascript instead and skipping the postback entirely:

如果对这两个问题的回答都是“是”,则您应该考虑在 javascript 中执行所有这些操作,并完全跳过回发:

<input type="radio" value="..." name="..." id="..."
    onclick="document.getElementById('tblSheet').style.backgroundColor = '#99ccff';" />

The reasoning here is that postbacks are incredibly slow compared to keeping everything on the client, and they also hurt the ability of your web app to scale as easily (more work on the server is, of course, bad for scalability). So it's faster for the user and less work for your server.

这里的原因是,与将所有内容都保存在客户端上相比,回发速度非常慢,而且它们还损害了您的 Web 应用程序轻松扩展的能力(当然,服务器上的更多工作对可扩展性不利)。因此,它对用户来说速度更快,而服务器的工作量更少。

However, you need to be careful to also not losefunctionality for users that have javascript disabled when moving work off the server. But if this postback was already triggered by your radio button then you were already dependent on javascript for this feature.

但是,在将工作移出服务器时,您需要注意不要丢失禁用 javascript 的用户的功能。但是,如果此回发已由您的单选按钮触发,那么您已经依赖 javascript 来实现此功能。

回答by Wagner

Like Joel told, JavaScript is a better solution if you only need to change colors but and if you transform your table into a server control (using runat=server in your table or another server control) don't forget to use an embedded code block in getElementById to retrieve the table id because ASP.NET rename id's when it renders the page.

就像 Joel 所说的那样,如果您只需要更改颜色,JavaScript 是更好的解决方案,但是如果您将表格转换为服务器控件(在表格或其他服务器控件中使用 runat=server)不要忘记使用嵌入式代码块在 getElementById 中检索表 id,因为 ASP.NET 在呈现页面时重命名 id。

In Joel's example it will look like:

在 Joel 的示例中,它将如下所示:

<input type="radio" value="..." name="..." id="..."    onclick="document.getElementById('<% =tblSheet.ClientID%>').style.backgroundColor = '#99ccff';" />