10
Answers

How to create background color with Literal Control in Asp.Net

Hi Guys,, I'm trying to make background color in repeater with the condition, data come from database and then bind to repeater. I'm use bootstrap class for the background color.

The condition is :

- If status_anggota "AKTIF" it will show background color green ("text-center bg bg-success") on repeater.

- If status_anggota "TIDAK AKTIF" it will show background color red "text-center bg bg-danger" on repeater.

how to do that ?. Anny help could be appriciate.

This is the code bihind

//This code not working
protected void StatusAnggota()
    {
        string HtmlStringAktif = "text-center bg bg-success";
        string HtmlStringTidakAktif = "text-center bg bg-danger";
        using (SqlConnection con = new SqlConnection(koneksi))
        {
            using (SqlCommand sqlcmd = new SqlCommand())
            {
                con.Open();
                sqlcmd.Connection = con;
                sqlcmd.CommandType = CommandType.Text;
                sqlcmd.CommandText = "Select * From dbo.tbl_anggota";
                using (SqlDataReader rdr = sqlcmd.ExecuteReader())
                {
                    if (rdr.Read())
                    {
                        new LiteralControl(HtmlStringAktif = rdr["status_anggota"].ToString());
                        SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
                        DataTable dt = new DataTable();
                        con.Close();
                        adp.Fill(dt);
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                    else
                    {
                        new LiteralControl (HtmlStringTidakAktif = rdr["status_anggota"].ToString());
                        SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
                        DataTable dt = new DataTable();
                        con.Close();
                        adp.Fill(dt);
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                }
            }
            con.Close();
        }
    }

This is the repeater

<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <table style="width: 100%;" id="myTable" class="table table-hover table-responsive table-borderless">
                <thead class="table table-dark text-center">
                    <tr>
                        <th>ID Anggota
                        </th>
                        <th>Nama Anggota
                        </th>
                        <th>Alamat
                        </th>
                        <th>Nomer Telepon
                        </th>
                        <th>Tanggal Bergabung
                        </th>
                        <th>Status Anggota
                        </th>
                        <th>Saldo (Rp.)
                        </th>
                        <th>Catatan
                        </th>
                        <th>#
                        </th>
                    </tr>
                </thead>
                <tbody>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td class="text-center">
                    <%#Eval("id_anggota")%>
                </td>
                <td style="width: 25%;" class="text-center">
                    <%#Eval("nama_anggota")%>
                </td>
                <td style="width: 25%;" class="text-center">
                    <%#Eval("alamat")%>
                </td>
                <td class="text-center">
                    <%#Eval("no_telpon")%>
                </td>
                <td class="text-center">
                    <%#Eval("tgl_join","{0: dd MMMM yyyy}")%>
                </td>
                <td style="width: 10%;">
                    <%#Eval("status_anggota")%>
                </td>
                <td style="width: 25%;" class="text-center">
                    <%#Eval("saldo","{0:Rp 0,00.00}")%>
                </td>
                <td style="width: 15%;" class="text-center">
                    <%#Eval("catatan")%>
                </td>
                <td>
                    <asp:LinkButton ID="LinkButton1" CommandArgument='<%#Eval("id_anggota") %>' class="fa fa-edit btn btn-warning btn-sm" OnClick="LinkButton1_Click" runat="server"> Detail</asp:LinkButton>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </tbody> 
            </table>
        </FooterTemplate>
    </asp:Repeater>

Answers (10)