How do you programmatically apply a css class to an asp.net control?

前端 未结 7 1626
余生分开走
余生分开走 2021-01-16 11:46

I\'m wondering how I can apply a CSS class to an ASP.NET Control (TextBox in this case) through the backend in C#. For example:



        
相关标签:
7条回答
  • 2021-01-16 12:14
    if(yourCondition) {
        var css = "myClass";
        if(String.IsNullOrWhiteSpace(element.CssClass))
            element.CssClass = css;
        else
            element.CssClass += " " + css;
    }
    
    0 讨论(0)
  • 2021-01-16 12:15

    you can use attribute of ui controls

    firstName.Attributes["css"]="Your Class Name";
    
    0 讨论(0)
  • 2021-01-16 12:17

    You can implement adding cssClass like

    firstName.CssClass = "input left selected";
    

    Or

    make onClick event for textbox

    <asp:TextBox ID="firstName" CssClass="input left" runat="server" Text="First Name" OnClick="firstName_Click"  />
    

    Then in code-behind:

    void firstName_Click(Object sender, EventArgs e) {
        MyTextBox.CssClass = "input left selected";
    }
    
    0 讨论(0)
  • 2021-01-16 12:19

    You can just do this:

     firstName.CssClass = "input left selected".
    

    If you want to append to any existing class names, do this:

    firstName.CssClass += " selected";
    
    0 讨论(0)
  • 2021-01-16 12:21

    It's just a string property of WebControl:

    firstName.CssClass += " selected";
    
    0 讨论(0)
  • 2021-01-16 12:28

    I think you could do that in the normal way something like below:

          if (value.Equals(true))
           {
                firstName.CSSClass = "input left selected";
           }
    

    Hope this helps!!

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