473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with Styles on ASP.NET WebControl

Hi everyone.

I could really use some help.

First, the backstory:
=====================
I *really* need a 3-state checkbox for my ASP.NET application. Specifically, I need one that lets me set the image for the checkbox portion so that the control conforms to my site's visual style, and that lets me set the style for the text in both the enabled and disabled states.

As you might expect, I couldn't find one. So I decided to roll my own.

Now, the problem:
=================
The control works exactly as I expect it to, except for one problem. The control has two properties of type Style: DisabledTextStyle and TextStyle. When I change either of those properties in any way, such as by setting the CSSCLASS property of the style, the change appears immediately in the designer. However, if I switch to HTML view or run the application, the style information is lost.

My theory:
==========
I should be doing something to make the style information stick to the ASP.NET code in the designer. I'm not doing that. However, I can't find any information on what I should be doing. (I used the MSDN samples.)

For your consideration:
=======================
The complete source code for the control follows below. Please paste it into a project and compile it. Then drop the control onto a Web Form and try to change the style. You'll see what I mean.

Also, if you see any way for me to improve the control, feel free to chime in. I would really like to release this to the community when it's finished, so the better it works, the happier I'll be.

The Code:
=========
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls

' This control's design is based on the Checkbox documentation
' from the Avalon documentation on MSDN at the following URL:
' http://winfx.msdn.microsoft.com/libr...x/checkbox.asp

Public Enum CheckboxState
Checked = 1
Unchecked = 0
Indeterminate = 2
End Enum

<DefaultProperty("Text"), _
DefaultEvent("CheckedChanged"), _
ToolboxData("<{0}:Checkbox runat=server></{0}:Checkbox>")> _
Public Class Checkbox
Inherits WebControl
Implements INamingContainer, IPostBackEventHandler

Const OnClickEventArgument As String = "onclick"

Event CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

#Region "Checked"
Public Property Checked() As Boolean
Get
Return Me.CheckedState = CheckboxState.Checked
End Get
Set(ByVal Value As Boolean)
Me.CheckedState = CheckboxState.Unchecked
End Set
End Property
#End Region

#Region "CheckedImageUrl"
<DefaultValue(""), Editor(GetType(ImageUrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property CheckedImageUrl() As String
Get
Return CStr(Me.ViewState("CheckedImageUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("CheckedImageUrl") = Value
End Set
End Property
#End Region

#Region "CheckedState"
<Bindable(True), Category("Appearance"), DefaultValue(GetType(CheckboxState), "-1")> _
Public Property CheckedState() As CheckboxState
Get
Return CType(Me.ViewState("CheckedState"), CheckboxState)
End Get
Set(ByVal Value As CheckboxState)
If Value <> Me.CheckedState Then
If Value = CheckboxState.Indeterminate Then
If ThreeState = False Then
Throw New ApplicationException("Invalid state.")
End If
End If
Me.ViewState("CheckedState") = Value
OnCheckedChanged()
End If
End Set
End Property
#End Region

#Region "CreateChildControls"
Protected Overrides Sub CreateChildControls()
CreateControlHeirarchy()
End Sub
#End Region

#Region "CreateControlHeirarchy"
Private Sub CreateControlHeirarchy()

Dim img As New ImageButton
Dim table As New table
Dim tr As New TableRow
Dim lbl As New Label
Dim tdImage As New TableCell
Dim tdText As New TableCell

If Me.Enabled Then
Select Case Me.CheckedState
Case CheckboxState.Checked
img.ImageUrl = Me.CheckedImageUrl
Case CheckboxState.Unchecked
img.ImageUrl = Me.UncheckedImageUrl
Case Else
img.ImageUrl = Me.IndeterminateImageUrl
End Select
img.Attributes.Add("onclick", "javascript:" & Page.GetPostBackEventReference(Me, OnClickEventArgument))
Else
img.ImageUrl = Me.DisabledImageUrl
End If

If Me.Site Is Nothing Then
lbl.Text = Me.Text
ElseIf Me.Site.DesignMode Then
' We're in the designer. Display the control name
' if we don't have any text yet.
If Me.Text = String.Empty Then
lbl.Text = "[" & Me.ClientID & "]"
Else
lbl.Text = Me.Text
End If
Else
lbl.Text = Me.Text
End If

If Me.Enabled Then
lbl.ApplyStyle(Me.TextStyle)
lbl.Attributes.Add("onclick", "javascript:" & Page.GetPostBackEventReference(Me, OnClickEventArgument))
Else
lbl.ApplyStyle(Me.DisabledTextStyle)
End If

table.Style.Clear()
table.CellPadding = 0
table.CellSpacing = 0
table.BorderStyle = BorderStyle.None
table.ApplyStyle(Me.ControlStyle)
table.Style.Add("border-collapse", "collapse")
table.Style.Add("cursor", "hand")

tr.Style.Clear()
tr.Style.Add("margin", "0")
tr.Style.Add("padding", "0")
tr.Style.Add("border", "none")
tr.Style.Add("cursor", "hand")

tdImage.Style.Clear()
tdImage.Style.Add("margin", "0")
tdImage.Style.Add("padding", "0")
tdImage.Style.Add("border", "none")
tdImage.Style.Add("cursor", "hand")

tdText.Style.Clear()
tdText.Style.Add("margin", "0")
tdText.Style.Add("padding", "0")
tdText.Style.Add("border", "none")
tdText.Style.Add("cursor", "hand")

table.Controls.Add(tr)
tr.Controls.Add(tdImage)
tr.Controls.Add(tdText)
tdImage.Controls.Add(img)
tdText.Controls.Add(lbl)

Controls.Clear()
Controls.Add(table)

End Sub
#End Region

#Region "DisabledImageUrl"
<DefaultValue(""), Editor(GetType(ImageUrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property DisabledImageUrl() As String
Get
Return CStr(Me.ViewState("DisabledImageUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("DisabledImageUrl") = Value
End Set
End Property
#End Region

#Region "DisabledTextStyle"
Private m_DisabledTextStyle As Style
Public Overridable ReadOnly Property DisabledTextStyle() As Style
Get
ensurechildcontrols()
If m_DisabledTextStyle Is Nothing Then
m_DisabledTextStyle = New Style
If istrackingviewstate Then
CType(m_DisabledTextStyle, IStateManager).TrackViewState()
End If
End If
Return m_DisabledTextStyle
End Get
End Property
#End Region

#Region "ImageStyle"
Private m_imageStyle As Style
Public Overridable ReadOnly Property ImageStyle() As Style
Get
If m_imageStyle Is Nothing Then
m_imageStyle = New Style
If istrackingviewstate Then
CType(m_imageStyle, IStateManager).TrackViewState()
End If
End If
Return m_imageStyle
End Get
End Property
#End Region

#Region "IndeterminateImageUrl"
<DefaultValue(""), Editor(GetType(ImageUrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property IndeterminateImageUrl() As String
Get
Return CStr(Me.ViewState("IndeterminateImageUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("IndeterminateImageUrl") = Value
End Set
End Property
#End Region

#Region "LoadViewState"
Protected Overrides Sub LoadViewState(ByVal savedState As Object)

If Not savedState Is Nothing Then

Dim items() As Object = CType(savedState, Object())

' First item is always the control's viewstate
If Not items(0) Is Nothing Then
MyBase.LoadViewState(items(0))
End If

' Next item should be our control's viewstate
If Not items(1) Is Nothing Then
CType(Me.TextStyle, IStateManager).LoadViewState(items(1))
End If

If Not items(2) Is Nothing Then
CType(Me.ImageStyle, IStateManager).LoadViewState(items(2))
End If

If Not items(3) Is Nothing Then
CType(Me.DisabledTextStyle, IStateManager).LoadViewState(items(3))
End If

End If

End Sub
#End Region

#Region "OnCheckedChanged"
Protected Sub OnCheckedChanged()
RaiseEvent CheckedChanged(Me, System.EventArgs.Empty)
End Sub
#End Region

#Region "OnClick"
Private Sub OnClick()

Dim state As CheckboxState = Me.CheckedState

If state = CheckboxState.Indeterminate Then
Me.CheckedState = CheckboxState.Unchecked
ElseIf state = CheckboxState.Unchecked Then
Me.CheckedState = CheckboxState.Checked
ElseIf ThreeState Then
Me.CheckedState = CheckboxState.Indeterminate
Else
Me.CheckedState = CheckboxState.Unchecked
End If

End Sub
#End Region

#Region "RaisePostBackEvent"
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackE vent
If eventArgument = OnClickEventArgument Then
OnClick()
End If
Stop
End Sub
#End Region

#Region "Render"
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
CreateControlHeirarchy()
Me.RenderContents(writer)
End Sub
#End Region

#Region "SaveViewState"
Protected Overrides Function SaveViewState() As Object

Dim baseState As Object = MyBase.SaveViewState
Dim textStyleState As Object
Dim imageStyleState As Object
Dim disabledTextStyle As Object
Dim stateObjects(3) As Object

textStyleState = CType(Me.TextStyle, IStateManager).SaveViewState()
imageStyleState = CType(Me.ImageStyle, IStateManager).SaveViewState()
disabledTextStyle = CType(Me.DisabledTextStyle, IStateManager).SaveViewState()

stateObjects(0) = baseState
stateObjects(1) = textStyleState
stateObjects(2) = imageStyleState
stateObjects(3) = disabledTextStyle

Return stateObjects

End Function
#End Region

#Region "Text"
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String
Get
Dim value As String = Me.ViewState("Text")
If value Is Nothing Then
value = String.Empty
End If
Return value
End Get
Set(ByVal Value As String)
Me.ViewState("Text") = Value
End Set
End Property
#End Region

#Region "TextStyle"
Private m_textStyle As Style
Public Overridable ReadOnly Property TextStyle() As Style
Get
If m_textStyle Is Nothing Then
m_textStyle = New Style
If IsTrackingViewState Then
CType(m_textStyle, IStateManager).TrackViewState()
End If
End If
Return m_textStyle
End Get
End Property
#End Region

#Region "ThreeState"
Public Property ThreeState() As Boolean
Get
Return CBool(Me.ViewState("ThreeState"))
End Get
Set(ByVal Value As Boolean)
If Value = False And Me.CheckedState = CheckboxState.Indeterminate Then
Me.CheckedState = CheckboxState.Unchecked
End If
Me.ViewState("ThreeState") = Value
End Set
End Property
#End Region

#Region "TrackViewState"
Protected Overrides Sub TrackViewState()
MyBase.TrackViewState()
CType(Me.DisabledTextStyle, IStateManager).TrackViewState()
CType(Me.TextStyle, IStateManager).TrackViewState()
End Sub
#End Region

#Region "UncheckedImageUrl"
<DefaultValue(""), Editor(GetType(ImageUrlEditor), GetType(System.Drawing.Design.UITypeEditor))> _
Public Property UncheckedImageUrl() As String
Get
Return CStr(Me.ViewState("UncheckedImageUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("UncheckedImageUrl") = Value
End Set
End Property
#End Region

End Class
Nov 19 '05 #1
0 1626

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steven (remove wax and invalid for reply) | last post by:
I'm moving an old page to a new host, and trying to modernize it at the same time. There are two little problems that are driving me nuts. 1. Validation of www.marzolian.com/index.html says: ...
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
0
by: Sky Sigal | last post by:
Hello: I'm having a little trouble understanding how to effectively offer Style control on the subelements of a Composite control. The book I have is pointing towards a certain set of steps that...
2
by: Logan | last post by:
Can the subject be done, or is setting of styles via styles sheets limited only to classic html controls? Quite obviously i am new to asp.net. thank you all.
1
by: Nathan Sokalski | last post by:
I want to make sure that the SelectedDate property of the Calendar control is later than the current date or that a certain checkbox is selected. I tried to use a CustomValidator control with the...
4
by: antonyliu2002 | last post by:
I am new to the .NET framework. I know this has been discussed many times in this group. I also read extensively here, however, I am in bad luck: none of the sample code provided in this forum...
3
by: J'son | last post by:
Guys, I have created a custom class that derives from DataList so that I can add some custom client side functionality into each new item row (<td>). Heres the class in its simplest form: ...
2
by: Peter Rilling | last post by:
Okay, I something weird is happening where I do not know if I am doing something wrong. I have a page that contains a custom webcontrol that I developed. This webcontrol basically loads a...
3
by: Sebastian Paul | last post by:
Hi, I registered a WebControlAdapter for any WebControl, that removes all ID attributes (I actually do not need them). Everything works as expected, but the DataList ignores any styles (ItemStyle,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.