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

前端 未结 7 1627
余生分开走
余生分开走 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:30

    Simply create an entry in the ViewDataDictionary for the class.

    The backend would look something like

    ViewData["selectedclass"] = <conditionalstatement> ? "selected" : "";
    

    And the view would be

        <asp:TextBox ID="firstName" CssClass="input left <%= ViewData["selectedclass"]%>" runat="server" Text="First Name" />  
    
    0 讨论(0)
提交回复
热议问题