locked
Can't change css cell (td) from behind code RRS feed

  • Question

  • User1343215443 posted

    how thow to change css from behind code if my td in listview

    <ItemTemplate runat="server">
                                <td id="backtab" runat="server" class="backtab">
                                    <asp:Label ID="unit_codeLabel" style="font-size:x-large" runat="server" Text='<%# Eval("unit_code") %>' /><br />
                                    status:
                                    <asp:Label ID="statusLabel" runat="server" Text='<%# Eval("status") %>' />
                                    <br />
                                </td>
                            </ItemTemplate>
                        </asp:ListView>

    my behind

    backtab.Attributes("class") = "NewClass";

    Wednesday, June 28, 2017 7:33 AM

All replies

  • User2119946224 posted

    Please try below:

    backtab.CssClass = "NewClass"

    Hope it helps. If yes please kindly mark as answer.

    Regards,

    Hasmukh

    Wednesday, June 28, 2017 9:51 AM
  • User-707554951 posted

    Hi  rifqiwqiw

    Working code as below:

     <style>
            .backtab {
                background-color:burlywood;
            }
            .newbacktab {
                  background-color:cadetblue;
            }
        </style>
    
       <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
                    ItemPlaceholderID="itemPlaceHolder1">
                    <LayoutTemplate>
                        <table>
                            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
                        </table>
                    </LayoutTemplate>
                    <GroupTemplate>
                        <tr>
                            <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
                        </tr>
                    </GroupTemplate>
                    <ItemTemplate>
                            <td id="backtab" runat="server" class="backtab">
                                    <asp:Label ID="unit_codeLabel" style="font-size:x-large" runat="server" Text='<%# Eval("ContactName") %>' /><br />
                                    status:
                                    <asp:Label ID="statusLabel" runat="server" Text='<%# Eval("Country") %>' />
                                    <br />
                                </td>
                    </ItemTemplate>
                </asp:ListView>

    CodeBehind:

     protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ContactName",typeof(string )),
                new DataColumn("Country",typeof(string )),});
                dt.Rows.Add("Maria Anders", "Germany");
                dt.Rows.Add("Ana Trujillo", "Mexico");
                lvCustomers.DataSource = dt;
                lvCustomers.DataBind();
                lvCustomers.DataSource = dt;
                lvCustomers.DataBind();
    foreach (ListViewDataItem item in lvCustomers.Items) { var td =(HtmlTableCell) item.FindControl("backtab"); td.Attributes["class"] = "newbacktab"; } }

    Best regard

    Cathy

    Thursday, June 29, 2017 5:58 AM