开发者

CssClass duplicates on an UpdatePanel update

开发者 https://www.devze.com 2022-12-27 07:41 出处:网络
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.

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开发者_如何学Go 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号