Click here to Skip to main content
15,039,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


What I have tried:

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>
Posted
Updated 5-Jul-21 1:09am
Comments
Richard Deeming 5-Jul-21 4:38am
   
new LiteralControl(HtmlStringAktif = rdr["status_anggota"].ToString());

Why are you creating a control instance only to throw it away?

You can set a local variable without creating a new control instance:
HtmlStringAktif = rdr["status_anggota"].ToString();


But since you never use that local variable anywhere, you might as well get rid of that line entirely.

You'll also find that having the same line in the else block will throw an exception, because there is no row to read the value from.
tri setia 5-Jul-21 7:05am
   
@Richard Deeming
Thanks so much for the explain sir,
the problem was solved.

1 solution

Problem Solved
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="OnItemDataBound">
        <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 runat="server" id="row">
                <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%;" class="text-center">
                    <%#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>


C#

The Code Bihind

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowData();
        }
    }
    public void ShowData()
    {
        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";
                SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
                con.Close();
                AutoNumberIDAnggota();
            }
        }
    }
    protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string HtmlStringAktif = "text-center bg bg-success";
            string HtmlStringTidakAktif = "text-center bg bg-danger";
            HtmlTableRow row = e.Item.FindControl("row") as HtmlTableRow;
            if ((e.Item.DataItem as DataRowView)["status_anggota"].ToString() == "AKTIF")
            {
                row.Attributes["class"] = HtmlStringAktif;
            }
            else
            {
                row.Attributes["class"] = HtmlStringTidakAktif;
            }
        }
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900