Gilad Adar
Gilad Adar

Reputation: 167

asp.net repeater for each row

I have a repeater that retrieve data from my db. some of the results are getting a null value in a specific column due to a join query and its fine.

I would like to go over each row and if the result is null for the specific row i want to change the css for this row.

Now for the code:

<asp:Repeater ID="repRequests" OnItemDataBound="repRequests_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:Label ID="lbltest" runat="server" Text='<%#Eval("val_name") %>'></asp:Label>
    </ItemTemplate>
</asp:Repeater>

C#:

if (!IsPostBack)
{
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("check_accepted", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@val_name", Session["valName"].ToString());
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        repRequests.DataSource = dr;
        repRequests.DataBind();
    }
}

How to write this ?

protected void repRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        **//what i want to get:**

        if (dr["accepted_id"] == Null) // a column from the db table
        {
            repRequests.attribute["class"] = "Some Class"
        }
    }
}

Thanks for the helpers !

Upvotes: 1

Views: 3049

Answers (3)

Kiran Hegde
Kiran Hegde

Reputation: 3681

You can try the following. If the data source is a DataReader object, you must cast e.Item.DataItem as type DBDataRecord (from System.Data.Common)

protected void repRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        System.Data.Common.DbDataRecord dataRow = e.Item.DataItem as System.Data.Common.DbDataRecord;
        if (dataRow["accepted_id"] == DBNull.Value || dataRow["accepted_id"] == null) // a column from the db table
        {
          // I am not sure how to you get repRequests. but you can find the control using e.Row.Item.FindControl() function
           repRequests.attribute["class"] = "Some Class";
        }
    }
}

EDIT Further to your questions, if you want to change the css of a perticular row, if some value is null, you cannot set css to repeater or repeater item directly. What you need to do is add a top level panel to the ItemTemplate like this

<ItemTemplate>
    <asp:Panel runat="server" ID="panelRow">
        <asp:Label ID="lbltest" runat="server" Text='<%#Eval("val_name") %>'></asp:Label>
        </asp:Panel>
</ItemTemplate>

Then you can change the css of the panel like below

protected void repRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item ||
                    e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    System.Data.Common.DbDataRecord dataRow = e.Item.DataItem as System.Data.Common.DbDataRecord;

                    if (dataRow["accepted_id"] == DBNull.Value || dataRow["accepted_id"] == null) // a column from the db table
                    {
                        Panel panelRow = e.Item.FindControl("panelRow") as Panel;
                        panelRow.CssClass = "yourcssclass";

                    }
                }
            }

Upvotes: 1

Hassan
Hassan

Reputation: 5430

Here is link example of DbDataRecord when you bind repeater control with sqldatareader.

Also note that repRequests.attribute["class"] = "Some Class" this means you are applying CSS Class to the repeater control. You need to change the CssClass label which is used as item of repeater control.

protected void repRequests_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
    {
         DbDataRecord dbr = (DbDataRecord)e.Item.DataItem;
         if( Convert.ToString(DataBinder.Eval(dbr, "accepted_id")) == null )
             ((Label)e.Item.FindControl("lbltest")).CssClass = "Some Class";
    }
}

Upvotes: 1

gkrishy
gkrishy

Reputation: 756

If not, you can use IsNull in your query.

SqlCommand cmd = new SqlCommand("select IsNull(something,zero) from sometable",connectionstring)
SqlDatareader dr = cmd.ExecuteReader();
while(dr.Read())
{
   if(dr.GetString(0)=="zero")
   {
        repRequests.attribute["class"] = "Some Class";
   }
}

Upvotes: 1

Related Questions