2011-02-01 42 views
0
<table style="width: 100%; background-color: #B5C7DE"> 

我有这个表,并想改变背景颜色,如果使用的诠释,他的服务器是黄色服务器上的SQL连接字符串是不是' pheddv20'asp.net设置表背景色dynamicaly总部设在SQL的ConnectionString

我将c#代码添加到页面加载事件来检查此并运行动态逻辑。连接字符串保存在web.config中,如通常所见...

这种类型的行为代码是什么样的?

+1

你可以获取公共属性的值来设置背景颜色,例如

;”>。为此,请创建一个名为BackColor的公共属性,并在您的加载事件中设置该值。 – Zachary2011-02-01 17:59:59

+0

我假设您已经将runat =“server”和id添加到此表格标记中,以便您可以在页面加载事件中访问它。对? – 2011-02-01 18:00:29

回答

1

这是更好地与CSS类,纪念表:

<style type="text/css"> 
.releasedata{background:red} 
.testdata{background:yellow} 
</style> 
<table class='<%= GetTableClassForEnvironment() %>'>...</table> 

在你你后面的代码会希望添加的方法

protected string GetTableClassForEnvironment() 
{ 
/* 
    parse environment string from web.config using ConfigurationManager.ConnectionStrings 
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx 
*/ 
} 

这不是在您的文章,但我猜你想拥有的表以不同的颜色显示取决于环境(即。调试/发布/ QA)。你应该看看使用web.config转换。如果你这样做,你可以在你的代码中使用条件编译语句,而不必依赖web.config解析。所以你的方法变成

protected string GetTableClassForEnvironment() 
{ 
#IF DEBUG 
return "releasedata" 
#ELSE 
return "testdata" 
#END IF 

} 
0

让你的表runat =“server”,然后你可以在你的代码后面访问style属性。

我可以建议一个替代方案吗?

dbtablecolor.Text = dbstring == "pheddv20" ? "white" : "yellow"; 

丑陋,绝对是一个黑客,但如果你把风格/母版页设置,然后你可以使用在整个应用程序相同的风格:

<style> 
.dbdependant 
{ 
    background: <asp:literal runat="server" id="dbtablecolor" />; 
} 
</style> 
<table class="dbdependant"> 
在后面的代码

然后需要...