Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to save a color value to Viewstate 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I'm building a custom web control and I want to save a color value to ViewState.

Like BackColor.

I've searched for this and it seems String is not the answer, but System.Drawing.Color converts to what?

I can save an object to the ViewState but not a color value. I know this can be done.

Thanks for any and all help.
 
how are you using BackColor? if you are exposing a public member. example:
Code:
public Color BackColor
{
   get {return this.Color;}
   set {this.Color = value;}
}
it should flow through viewstate automatically. if you want more fine grained control look into overriding SaveControlState, SaveViewState, GetControlState and GetViewState to store the value. If Color cannot be serialized you will need to conver it to hex or rgb values and store that in viewstate.

however things like color, layout, etc. should be managed via CSS, not the webform server property values. it will be much easier to manage the UI if you embrace the environment with which you are developing.

webforms can deliver the data (very poorly, but it can) to the html template (markup) but the codebehind should not control the template. javascript and CSS can control the layout and styling of html much easier than webforms. it's like learning another language, but that language is very effecient at a specific task.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
You can just store the name of the color and then load it back in e.g.
Code:
        Dim c As System.Drawing.Color = Drawing.Color.Aqua
        ViewState.Add("MyColor", c.Name)
        Dim d As System.Drawing.Color = System.Drawing.Color.FromName(ViewState("MyColor"))
        Response.Write("Your color was " & d.Name)
However, as Jason said, you should always try to learn the language of the underlying technology as what you are using is simply a wrapper for CSS, and there is so much more you can do if you understand what is actually happening rather than letting a tool do the work for you.


Mark,

Darlington Web Design
Experts, Information, Ideas & Knowledge
ASP.NET Tips & Tricks
 
Hi guys.
Sorry for the confusing post.

The way the control is now, we have a label that gos on all of our forms to give the user feedback as to what they did.

I also use CSS to color it. If I can do it in CSS, I never do it at the server level.
But I wanted an "all on one solution" that could be drug and dropped onto a form that had everything built in.

For example, when creating this by hand, I have a class that drives a simple label. The class has a few methods.
Also the CSS in the page that gos along with it to display colors.

We figured if I could create a custom control with set properties, then it could be a tool box drag and drop.
All of the code comes with it, including what colors it displays.


Works like this:

infoLabel.ShowMessage("whatever message", isError)

isError is a boolean

If the message is empty, the label's visible property is false.

If isError is true, then the back and fore colors may be red on pink.

If isError is false the back and fore colors may be blue on white.

These depend on what developer is using it in their application.
If the app has a greenish tone, you would not want a blue label. :)

This was to be a test to see if it was worth converting some of our pain in the butt stuff like this to server controls.

Sorry if I was unclear.
 
Hi Mark. Yes, with one change it worked great.

Only had 1 error because of the implicit conversion:
Dim d As System.Drawing.Color = System.Drawing.Color.FromName(ViewState("MyColor"))

Fixed that.
If anyone else comes across this, I'm posting my code:

At the top of the class I declared some variables:

Dim vErrorBackColor As System.Drawing.Color

Code:
    Public Property ErrorBackColor() As System.Drawing.Color
      Get
        Dim o As Object = ViewState("ErrorBackColor")
        If o Is Nothing Then
          vErrorBackColor = lblMessage.BackColor
          Return vErrorBackColor
        Else
          vErrorBackColor = System.Drawing.Color.FromName(o.ToString)
          Return vErrorBackColor
        End If
      End Get

      Set(ByVal value As System.Drawing.Color)
        ViewState.Add("ErrorBackColor", value.Name)
        vErrorBackColor = value
        lblMessage.BackColor = value
      End Set

    End Property

If the user tries to get the property without first setting it, then the code will take care of itself and send back the default value for the label.


I should mention that this is the first web control I'm building, so it's more of a proof of concept. I made a few custom controls back in VB 5 days but nothing for web/net. It's confusing but interesting.
I'm also learning C# at the same time. (loving it)
 
while this works, I find this too complex. Not to mention I have to manage the magic string ("ErrorBackColor") in 2 places. this would increase exponentially with each property I need to store. I would reduce code repetition and simplify the logic with something like:
Code:
public Color ErrorBackColor 
{
   get { return lblMessage.BackColor; }
   set { lblMessage.BackColor = value; }
}

protected override void SaveViewState(object obj)
{
   var array = (object[])obj;
   base.SaveViewState(new object[]{array, ErrorBackColor});
}

protected override object GetViewState()
{
   var array = (object[])base.GetViewState();
   ErrorBackColor = (Color)array[1];
   return array[0];
}
public properties are just encapsulating control properties. The management of viewstate is encapsulated by get/save viewstate. No magic strings (although indexing is important). As more properties requiring viewstate storage are created I just add them to the array of save/get viewstate.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason I tried your code and that doesn't work for me.

VS says: SaveViewState can not be declared Overrides because it does not override a sub in this base class.

I had to port it by hand to vb. The online converter made a mess of it.

I should have done this in C#. :(

I'm always looking for new ways to do things. Shame I have no other developers in my building to geek with.
 
I didn't expect the code to be flawless. it's been years since I used webforms and I wrote this off the top of my head. the idea works. the members are there, just named/defined differently.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I hate webforms myself. Benn doing web for 15 years and as soon as I can get out of this I am.
Applications were so much easier to write.
 
I love the web, I hate webforms.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top