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.
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.
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:
if (rdoStatus.SelectedValue == "181003")
{
tblSheet.BgColor = "#ff9a9a";
}
else
{
tblSheet.BgColor = "#99ccff";
}
Place runat="server" in the table tag
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"
}
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:
<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.
However, you need to be careful to also not lose functionality 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With