Session variable not taking effect in asp.net
I have a session variable in my asp.net application. The session variable holds a value from the database, that reflects a customized HTML color value.
In my application, I have an asp button with server side codebtnContinue.BackColor = System.Drawing.Color.FromName(Session("ContinueColor"))
Issue: However, when I run开发者_开发百科 the application, the color value is not being reflected in the button.
I did double check, and the session variable does hold the correct value.
There are other objects, that use session variables to display colors, and they are working fine.How can I resolve this issue?
Update: When I force a color "btnContinue.BackColor = Drawing.Color.Blue", that works perfectly fine.
If it's a hex code, you might want to use ColorTranslator instead:
btnContinue.BackColor = System.Drawing.ColorTranslator.FromHtml(Session("ContinueColor").ToString());
Looking at the color information you posted in your comment, I think you just need to cast the session object as type Color:
btnContinue.BackColor = DirectCast(Session("ContinueColor"), System.Drawing.Color)
EDIT
I found the solution:
btnContinue.BackColor = System.Drawing.Color.FromName("{Name=48E8DD, ARGB=(0, 0, 0, 0)}")
In your case, it would be:
btnContinue.BackColor = System.Drawing.Color.FromName(Session("ContinueColor").ToString())
Which part of the page life cycle are you setting the color? Maybe the session data has not been read yet? Session should be ready after PageLoad.
I recommend setting a break point immediately after this line of code to make sure nothing else is overriding it - theme.
btnContinue.BackColor = System.Drawing.Color.FromName(Session("ContinueColor"))
精彩评论