views:

107

answers:

1

I've got a simple user control in my ASP.NET Webforms project which inherits from the LinkButton. It's got a property to change the size, which just adds some predefined CSS classes to the control.

Protected Overrides Sub CreateChildControls()
    Dim SizeClass As String = String.Empty
    If Size = SizeEnum.Large Then
        SizeClass = "large"
    Else
        SizeClass = "small"
    End If

    Me.CssClass += " button " + SizeClass

    Me.Controls.Add(New LiteralControl(String.Format("<span class=""l"">{0}</span><span class=""r""></span><span class=""clear""></span>", Me.Text)))
    MyBase.CreateChildControls()
End Sub

Pretty simple, right? So when it renders the class property is something like class="button small".

When this control is placed inside an update panel along with some other things, when the update panel updates the class property for every one of these controls becomes class=" button small button small button small button small button small button small button small button small button small button small button small button small button small"

Which is a little bit rediculous. Any idea on why this is happening?

+2  A: 

Try changing

Me.CssClass += " button " + SizeClass

to

Me.CssClass = " button " + SizeClass

Each time the control renders, the CssClass is stored in ViewState of the control. So when it renders it keeps appending.

Chris
Well, I still would like the ability to add whatever CssClass I might need to on the front end, which is why I'm adding to the existing CssClass instead of just setting it.
Chris Barr
You could try disabling the ViewState of the control?
Chris
Ah yes, thanks. it was a viewstate issue and disabling that fixed it!
Chris Barr