简体   繁体   English

如何动态更改CSS样式

[英]How to change the css styles dynamically

I am trying to change the css style of the repeater to a different color on different status. 我试图将转发器的css样式更改为不同状态的不同颜色。 The expected output is to print 6 different statuses like: In progress, complete, withdrawn so on.. Currently it has a single color, so it shows one color only. 预期输出是打印6种不同的状态,如:正在进行,完成,撤消等等。目前它有一种颜色,所以它只显示一种颜色。 the challenging part here is to change the color dynamically based on the status. 这里具有挑战性的部分是根据状态动态改变颜色。 how do I achieve this? 我怎么做到这一点? Currently it pulls the style based on "status noAction text-center" class below. 目前,它基于下面的“状态noAction文本中心”类来提取样式。 Should I make any changes in the code behind or is it just a front end css change. 我应该对后面的代码进行任何更改,还是仅仅是前端css更改。 Can someone provide me an example please. 请有人给我一个例子。

.aspx code: .aspx代码:

<div class="row">
                <asp:Repeater ID="rptStatuses" runat="server">
                    <ItemTemplate>
                        <div class="col-md-4 col-sm-4">
                            <div class="status noAction text-center">
                                <div class="banner">

                                    <asp:Label runat="server"> <%# Eval("ID") %></asp:Label>
                                </div>
                                <div class="label"><%# Eval("Name") %></div>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>

.cs code relevant part .cs代码相关部分

private void GB()
        {

            var surveyId = 55;
            var stateLabels = _manageDatasets.GetStateLabels(surveyId);
            List<Status> statusesList = new List<Status>();
            foreach (var sl in stateLabels)
            {
                if (sl.Key != -1)
                    statusesList.Add(new Status { ID = sl.Key.ToString(), Name = sl.Value }
                  );
            }
            this.rptStatuses.DataSource = statusesList;
            this.rptStatuses.DataBind();
}

I think what you want to do is change the class in the your html and just use the embedded code to be able to dynamically change the banner color, etc. based on the status. 我想你想要做的是改变你的html中的类,只需使用嵌入的代码就可以根据状态动态改变横幅颜色等。 The embedded code that you want would look something like this - 您想要的嵌入式代码看起来像这样 -

<div class="col-md-4 col-sm-4">
    <% if(status == "noAction") { %>
        <div class="status noAction text-center">
            <div class="banner">

                <asp:Label runat="server"> <%# Eval("ID") %></asp:Label>
            </div>
            <div class="label"><%# Eval("Name") %></div>
        </div>
    <% } %>

If you have any column in you table from where repeater is getting datasource then that would be pretty easy to do something like that 如果您在表中有任何列,其中转发器正在获取数据源,那么这样做很容易

 <asp:Repeater ID="rptStatuses" runat="server">
                <ItemTemplate>
                    <div class="col-md-4 col-sm-4">
                        <div class='<%# Convert.toInt32(eval("ActionColumnName"))==1 ? "status In progress text-center" : Convert.toInt32(eval("ActionColumnName"))==2 ? "status complete  text-center" : Convert.toInt32(eval("ActionColumnName"))==3 ? "status withdrawn text-center" : "status noAction text-center" '>
                            <div class="banner">

                                <asp:Label runat="server"> <%# Eval("ID") %></asp:Label>
                            </div>
                            <div class="label"><%# Eval("Name") %></div>
                        </div>
                    </div>
                </ItemTemplate>
            </asp:Repeater>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM