Kaushal
Kaushal

Reputation: 11

How do I change Button Text color onclick event(asp.net, C#)using Css file?

I have CSS classfile. So when I click on button the buttontext should be in green.

.buttongreen
{
    color:green;    
}

Upvotes: 0

Views: 16223

Answers (5)

Amna
Amna

Reputation: 611

It can be done without changing any cssClass. You can change your button text color like this

void OnClick1(object sender, RoutedEventArgs e)
{
        btn.Forecolor = System.Drawing.Color.Green;
}

Or you can change your button background color like this:

void OnClick1(object sender, RoutedEventArgs e)
{
    btn.Backgroundcolor = System.Drawing.Color.Green;
}

Upvotes: 2

Touseef Rehman
Touseef Rehman

Reputation: 125

Change the foreground color of your button text from OnClick event.

button.Forecolor = System.Drawing.Color.Green;

I hope this will work for you without CSS.

Upvotes: 1

killer
killer

Reputation: 602

You can do it in two ways:

  1. Using javascript

    <script type="text/javascript">
    
    function changeColor()
    

    {

    document.getElementById("buttonName").className = "MyClass";
    

    }

    <script>
    

call this function OnClientClick

  1. In code behind

    btnName.Attributes.Add("class", "buttongreen");

Upvotes: 1

Sayali Kini
Sayali Kini

Reputation: 26

I have updated my answer using cssClass OF button. Not sure but this may help you ..

protected void button_Click(object sender, EventArgs e)
{
    button.CssClass = "buttongreen";
}

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

If you only want to change color of button in code behind than do this

btnName.Style.Add(HtmlTextWriterStyle.Color, "green");

Here btnName is ID of button.

And if you want to add a class than do this

btnName.Attributes.Add("class", "buttongreen");

Upvotes: 1

Related Questions