Button background colour

前端 未结 6 1471

I need to change the background colour of a button using C# code (Visual Studio 2008).

I saw some people recommending the inclusion of a directive: us

相关标签:
6条回答
  • 2021-01-21 12:37
    Button1.BackColor = System.Drawing.Color.Red; 
    
    0 讨论(0)
  • 2021-01-21 12:38
    using System.Drawing;
    

    allows you to use

    Color.Red;
    
    0 讨论(0)
  • 2021-01-21 12:42

    On PageLoad try this,

    Button1.Style.Add("background-color", "green");

    Use a method to write a condition when you want to change the color of the button. if condition is true use above code to change the color of the button as you prefer.

    0 讨论(0)
  • 2021-01-21 12:47

    try this

    button.BackColor = Color.Red
    
    0 讨论(0)
  • 2021-01-21 12:51

    Although that property is technically available to all WebControls, it may be ignored for buttons (I haven't confirmed). This, of course, is assuming your project is ASP.NET. Having said that, if it DOES happen to work for you, I highly advise that you test this in other browsers as it may be MS specific.

    From MSDN:

    This property will render for only certain controls. For example, Table, Panel, DataGrid, Calendar, and ValidationSummary will render this property. It will also work for CheckBoxList, RadioButtonList and DataList if their RepeatLayout property is RepeatLayout.Table, not RepeatLayout.Flow.

    Source: WebControl.BackColor Property

    However, a more flexible and widely practiced method of achieving this is to use CSS.

    0 讨论(0)
  • 2021-01-21 13:01

    This should change your button background color to Red

    yourButtonName.BackColor = Color.Red;
    

    You need to include System.Drawing namespace as Color class belongs to that. Like this

    using System.Drawing;
    

    And ofcourse you need to add the reference to System.Drawing DLL in your project to use this namespace and Color class.

    enter image description here

    0 讨论(0)
提交回复
热议问题