473,729 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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: DisabledTextSty le 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.Componen tModel
Imports System.Web.UI
Imports System.Web.UI.D esign
Imports System.Web.UI.W ebControls

' 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

<DefaultPropert y("Text"), _
DefaultEvent("C heckedChanged") , _
ToolboxData("<{ 0}:Checkbox runat=server></{0}:Checkbox>") > _
Public Class Checkbox
Inherits WebControl
Implements INamingContaine r, IPostBackEventH andler

Const OnClickEventArg ument As String = "onclick"

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

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

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

#Region "CheckedSta te"
<Bindable(True) , Category("Appea rance"), DefaultValue(Ge tType(CheckboxS tate), "-1")> _
Public Property CheckedState() As CheckboxState
Get
Return CType(Me.ViewSt ate("CheckedSta te"), CheckboxState)
End Get
Set(ByVal Value As CheckboxState)
If Value <> Me.CheckedState Then
If Value = CheckboxState.I ndeterminate Then
If ThreeState = False Then
Throw New ApplicationExce ption("Invalid state.")
End If
End If
Me.ViewState("C heckedState") = Value
OnCheckedChange d()
End If
End Set
End Property
#End Region

#Region "CreateChildCon trols"
Protected Overrides Sub CreateChildCont rols()
CreateControlHe irarchy()
End Sub
#End Region

#Region "CreateControlH eirarchy"
Private Sub CreateControlHe irarchy()

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.C hecked
img.ImageUrl = Me.CheckedImage Url
Case CheckboxState.U nchecked
img.ImageUrl = Me.UncheckedIma geUrl
Case Else
img.ImageUrl = Me.Indeterminat eImageUrl
End Select
img.Attributes. Add("onclick", "javascript :" & Page.GetPostBac kEventReference (Me, OnClickEventArg ument))
Else
img.ImageUrl = Me.DisabledImag eUrl
End If

If Me.Site Is Nothing Then
lbl.Text = Me.Text
ElseIf Me.Site.DesignM ode 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.GetPostBac kEventReference (Me, OnClickEventArg ument))
Else
lbl.ApplyStyle( Me.DisabledText Style)
End If

table.Style.Cle ar()
table.CellPaddi ng = 0
table.CellSpaci ng = 0
table.BorderSty le = BorderStyle.Non e
table.ApplyStyl e(Me.ControlSty le)
table.Style.Add ("border-collapse", "collapse")
table.Style.Add ("cursor", "hand")

tr.Style.Clear( )
tr.Style.Add("m argin", "0")
tr.Style.Add("p adding", "0")
tr.Style.Add("b order", "none")
tr.Style.Add("c ursor", "hand")

tdImage.Style.C lear()
tdImage.Style.A dd("margin", "0")
tdImage.Style.A dd("padding", "0")
tdImage.Style.A dd("border", "none")
tdImage.Style.A dd("cursor", "hand")

tdText.Style.Cl ear()
tdText.Style.Ad d("margin", "0")
tdText.Style.Ad d("padding", "0")
tdText.Style.Ad d("border", "none")
tdText.Style.Ad d("cursor", "hand")

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

Controls.Clear( )
Controls.Add(ta ble)

End Sub
#End Region

#Region "DisabledImageU rl"
<DefaultValue(" "), Editor(GetType( ImageUrlEditor) , GetType(System. Drawing.Design. UITypeEditor))> _
Public Property DisabledImageUr l() As String
Get
Return CStr(Me.ViewSta te("DisabledIma geUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("D isabledImageUrl ") = Value
End Set
End Property
#End Region

#Region "DisabledTextSt yle"
Private m_DisabledTextS tyle As Style
Public Overridable ReadOnly Property DisabledTextSty le() As Style
Get
ensurechildcont rols()
If m_DisabledTextS tyle Is Nothing Then
m_DisabledTextS tyle = New Style
If istrackingviews tate Then
CType(m_Disable dTextStyle, IStateManager). TrackViewState( )
End If
End If
Return m_DisabledTextS tyle
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 istrackingviews tate Then
CType(m_imageSt yle, IStateManager). TrackViewState( )
End If
End If
Return m_imageStyle
End Get
End Property
#End Region

#Region "IndeterminateI mageUrl"
<DefaultValue(" "), Editor(GetType( ImageUrlEditor) , GetType(System. Drawing.Design. UITypeEditor))> _
Public Property IndeterminateIm ageUrl() As String
Get
Return CStr(Me.ViewSta te("Indetermina teImageUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("I ndeterminateIma geUrl") = Value
End Set
End Property
#End Region

#Region "LoadViewSt ate"
Protected Overrides Sub LoadViewState(B yVal savedState As Object)

If Not savedState Is Nothing Then

Dim items() As Object = CType(savedStat e, Object())

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

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

If Not items(2) Is Nothing Then
CType(Me.ImageS tyle, IStateManager). LoadViewState(i tems(2))
End If

If Not items(3) Is Nothing Then
CType(Me.Disabl edTextStyle, IStateManager). LoadViewState(i tems(3))
End If

End If

End Sub
#End Region

#Region "OnCheckedChang ed"
Protected Sub OnCheckedChange d()
RaiseEvent CheckedChanged( Me, System.EventArg s.Empty)
End Sub
#End Region

#Region "OnClick"
Private Sub OnClick()

Dim state As CheckboxState = Me.CheckedState

If state = CheckboxState.I ndeterminate Then
Me.CheckedState = CheckboxState.U nchecked
ElseIf state = CheckboxState.U nchecked Then
Me.CheckedState = CheckboxState.C hecked
ElseIf ThreeState Then
Me.CheckedState = CheckboxState.I ndeterminate
Else
Me.CheckedState = CheckboxState.U nchecked
End If

End Sub
#End Region

#Region "RaisePostBackE vent"
Public Sub RaisePostBackEv ent(ByVal eventArgument As String) Implements System.Web.UI.I PostBackEventHa ndler.RaisePost BackEvent
If eventArgument = OnClickEventArg ument Then
OnClick()
End If
Stop
End Sub
#End Region

#Region "Render"
Protected Overrides Sub Render(ByVal writer As System.Web.UI.H tmlTextWriter)
CreateControlHe irarchy()
Me.RenderConten ts(writer)
End Sub
#End Region

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

Dim baseState As Object = MyBase.SaveView State
Dim textStyleState As Object
Dim imageStyleState As Object
Dim disabledTextSty le As Object
Dim stateObjects(3) As Object

textStyleState = CType(Me.TextSt yle, IStateManager). SaveViewState()
imageStyleState = CType(Me.ImageS tyle, IStateManager). SaveViewState()
disabledTextSty le = CType(Me.Disabl edTextStyle, IStateManager). SaveViewState()

stateObjects(0) = baseState
stateObjects(1) = textStyleState
stateObjects(2) = imageStyleState
stateObjects(3) = disabledTextSty le

Return stateObjects

End Function
#End Region

#Region "Text"
<Bindable(True) , Category("Appea rance"), DefaultValue("" )> Property [Text]() As String
Get
Dim value As String = Me.ViewState("T ext")
If value Is Nothing Then
value = String.Empty
End If
Return value
End Get
Set(ByVal Value As String)
Me.ViewState("T ext") = 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 IsTrackingViewS tate Then
CType(m_textSty le, 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.ViewSt ate("ThreeState "))
End Get
Set(ByVal Value As Boolean)
If Value = False And Me.CheckedState = CheckboxState.I ndeterminate Then
Me.CheckedState = CheckboxState.U nchecked
End If
Me.ViewState("T hreeState") = Value
End Set
End Property
#End Region

#Region "TrackViewState "
Protected Overrides Sub TrackViewState( )
MyBase.TrackVie wState()
CType(Me.Disabl edTextStyle, IStateManager). TrackViewState( )
CType(Me.TextSt yle, IStateManager). TrackViewState( )
End Sub
#End Region

#Region "UncheckedImage Url"
<DefaultValue(" "), Editor(GetType( ImageUrlEditor) , GetType(System. Drawing.Design. UITypeEditor))> _
Public Property UncheckedImageU rl() As String
Get
Return CStr(Me.ViewSta te("UncheckedIm ageUrl"))
End Get
Set(ByVal Value As String)
Me.ViewState("U ncheckedImageUr l") = Value
End Set
End Property
#End Region

End Class
Nov 19 '05 #1
0 1642

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

Similar topics

7
1778
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: Line 33, column 5: element "NOTE" undefined <note>Last revised: May 29, 2005</note> "You have used the element named above in your document, but the document type you are using does not define an element of that name."
0
1598
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 can help me ... My environment: VS 2003 v7.1.3088, Win2K v5.0.2195 SP3, IE6 v6.0.2800.1106 browser. I have a class (C3Menu) derived from WebControl, with a property (MenuItems) that is a collection of menu items. The collection property is...
0
1047
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 appears to save the info TWICE...but I am not sure....could someone take a look? The steps suggested are approximately: a) Create a webcontrol that has two inner Controls (a TextBox and Button) b) provide public properties for each inner...
2
1030
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
1501
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 following code: Private Sub validTimeFrame_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles validTimeFrame.ServerValidate If chkUnknown.Checked OrElse...
4
1396
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 worked for me. What I want to do is really simple. I simply want to iterate through all controls of the current page and create session objects for TextBoxes and CheckBoxes.
3
3258
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: public class MyDataList : DataList { public string MyValue1 = "alert('Hey there!');"; public string MyValue2 = "alert('Hey there yourself!');";
2
2541
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 UserControl (ascx) using the LoadControl method and writing it out to the browser. Page --> custom webcontrol --> usercontrol (via LoadControl) --> stream to browser.
3
2236
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, SelectedItemStyle etc.). The DataList works as usual, i.e. it uses the SelectedItemTemplate for the selected item. Other controls, such as the TreeView, apply their styles properly. Any idea what's going wrong? It seems like a bug for me....
0
8932
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8767
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6028
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3246
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2702
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.