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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

web user control, state of properties?

Status
Not open for further replies.

ThatRickGuy

Programmer
Oct 12, 2001
3,841
US
Hey Guys,
I have a custom user control. This control has a number of properties I have added to it. I'm having a mind blank this morning as to how to get those properties to retain their state over a post back.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hrm... ViewState is set to true on the control on the page I have it on. Interestingly, it looks like some of the properties are being maintained. The control contains an image button that it controls the images for based on the controls binary state (true/false) and mouse down. If I throw in some debugging lines, I can see the ImageNormal property is returning the correct value, but IsState is always returning false. Since it's returning some values correctly, this has to be a mistake in my code someplace. Anyone want to take a quick glance at it and play "Find Rick's Mistake"? ;)

Code:
<System.ComponentModel.DefaultEvent("bsibClick")> Partial Class wucBinaryStateImageButton
    Inherits System.Web.UI.UserControl

#Region " Declarations"
    Private _blnIsState As Boolean
    Private _sNormal As String
    Private _sDown As String
    Private _sIsNormal As String
    Private _sIsDown As String
#End Region

#Region " Properties"
    Public Property Height() As Integer
        Get
            Return ib1.Height.Value
        End Get
        Set(ByVal value As Integer)
            ib1.Height = value
        End Set
    End Property

    Public Property Width() As Integer
        Get
            Return ib1.Width.Value
        End Get
        Set(ByVal value As Integer)
            ib1.Width = value
        End Set
    End Property

    Public Property IsState() As Boolean
        Get
            Return _blnIsState
        End Get
        Set(ByVal value As Boolean)
            _blnIsState = value
        End Set
    End Property

    Public Property ImageNormal() As String
        Get
            Return _sNormal
        End Get
        Set(ByVal value As String)
            _sNormal = value
        End Set
    End Property

    Public Property ImageDown() As String
        Get
            Return _sDown
        End Get
        Set(ByVal value As String)
            _sDown = value
        End Set
    End Property

    Public Property ImageIsNormal() As String
        Get
            Return _sIsNormal
        End Get
        Set(ByVal value As String)
            _sIsNormal = value
        End Set
    End Property

    Public Property ImageIsDown() As String
        Get
            Return _sIsDown
        End Get
        Set(ByVal value As String)
            _sIsDown = value
        End Set
    End Property

#End Region

    Protected Sub ib1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ib1.PreRender
        If IsState Then
            ib1.ImageUrl = ImageIsNormal
            ib1.Attributes.Add("onmousedown", "this.src = '" & ImageIsDown & "';")
            ib1.Attributes.Add("onmouseup", "this.src = '" & ImageIsNormal & "';")
            ib1.Attributes.Add("onmouseout", "this.src = '" & ImageIsNormal & "';")
        Else
            ib1.ImageUrl = ImageNormal
            ib1.Attributes.Add("onmousedown", "this.src = '" & ImageDown & "';")
            ib1.Attributes.Add("onmouseup", "this.src = '" & ImageNormal & "';")
            ib1.Attributes.Add("onmouseout", "this.src = '" & ImageNormal & "';")
        End If
    End Sub

    Protected Sub ib1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ib1.Click
        IsState = Not IsState
        RaiseEvent bsibClick()
    End Sub

    Public Event bsibClick()
End Class

Usage is like:
Code:
    <uc2:wucBinaryStateImageButton ID="WucBinaryStateImageButton1" runat="server" 
        Height="30"
        Width="30" 
        ImageDown="Images/btn_RecordH.PNG"
        ImageIsDown="Images/btn_RecordOnH.PNG"
        ImageIsNormal="Images/btn_RecordOn.PNG"
        ImageNormal="Images/btn_Record.PNG"        
        EnableViewState="true"  />

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Well, as a temporary kludge, I stuck this in the click event:

Code:
Boolean.TryParse(Me.Text, IsState)
IsState = Not IsState
Me.Text = Me.IsState

I converted the class from a web user control into a custom control that inherits from ImageButton. That gave me the .Text property that is being correctly saved in view state. But it also means that if a user hovers over the button, they get a pop-up that says 'true' or 'false'. It's good enough for development, but I'm going to have to figure this one out eventually.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Ahh well, I replaced the IsState property with this:

Code:
        Public Property IsState() As Boolean
            Get
                Return ViewState.Item("IsState")
            End Get
            Set(ByVal value As Boolean)
                ViewState.Item("IsState") = value
            End Set
        End Property

It works. I don't understand why the other custom properties are retaining their value while IsState was not, but this will do.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top