Iladarsda
Iladarsda

Reputation: 10696

VB.net add css class to clicked element

How to add CssClass to clicked element?

Protected Sub ShowButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowButton.Click

    // Add "active" CssClass to ShowButton 

End Sub

Is it possible to reference to element by this ?

Upvotes: 0

Views: 3561

Answers (1)

Curtis
Curtis

Reputation: 103348

The sender parameter is referring to the control which triggered this method (ie: the sender of the event).

Therefore you can change the properties of this object.

Assming ShowButton is a Button control. If not, change Button to whatever the type of the control is:

Protected Sub ShowButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowButton.Click

    CType(sender, Button).CssClass = "active" 

End Sub

If your control only has the one class, you can easily remove it by doing the following:

CType(sender, Button).CssClass = "" 

However, if you have multiple classes it can get more complicated. You could do:

CType(sender, Button).CssClass = CType(sender, Button).CssClass.Replace("active", "")

This only replaces active in the CssClass string property with a blank string. This works fine unless you have a class like reactive as well. This would then be changed to re.

Upvotes: 3

Related Questions