Solved

How to set repeater dataitem value to string variable in codebehind

Posted on 2010-11-16
6
2,040 Views
Last Modified: 2012-05-10
I have a repeater I am databinding to the results of a query that pulls 1 record from the database in my codebehind. I want to take the value of one of the dataitems in the repeater and assign it to a variable.  I then want to use the variable in the table that loads the reader in the aspx file to determine whether to change the font color of the row.  

I have searched and not been able to find it.  So if someone could provide me a link to an example or the example itself that would be great.  

I am new to the .net environment so if what I ask is worth more points than I am assigning please say so.

Thanks.

0
Comment
Question by:thamilto0410
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
ddayx10 earned 400 total points
ID: 34151933
Well I don't know of an example page of that, but I wrote a sample that may let you see what to do. Basically you want to use the Repeater_ItemDataBound event to check each "row" as it is being built, run some logic against it, then make a change to the color.

I have built a repeater and bound it to a listitemcollection object (just for ease). In the repeater there is an itemtemplate containing a div as a wrapper, and two spans containing my dataitems. I find the "value" span, check if it is an even number and if it is change it's color to green.

Entire code pasted below. You can cut/paste into a new page to run it stand-alone.
*******ASPX PAGE********
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="RepeaterColorRow.aspx.vb" Inherits="RepeaterColorRow" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="Rp1" runat="server">
		<ItemTemplate>
			<div id="DivToColor" runat="server">
			<span><%#Eval("text")%></span>
			<span><%#Eval("value")%></span>
			</div>
		</ItemTemplate>
    </asp:Repeater>
    </form>
</body>
</html>



*****ASPX.VB PAGE****
Partial Class RepeaterColorRow
    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Just need something to bind to
        Dim _BindingList As New ListItemCollection
        _BindingList.Add(New ListItem("item1", "0"))
        _BindingList.Add(New ListItem("item2", "1"))
        _BindingList.Add(New ListItem("item3", "2"))

        'Bind to the collection above (note "value and text" are the bindable attribute names here)
        Rp1.DataSource = _BindingList
        Rp1.DataBind()

    End Sub


    Protected Sub Rp1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Rp1.ItemDataBound
        'must make sure it is an item or altitem template (not header or footer etc)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            'Get the databound item "value" and put into a variable
            'NOTE: "value" is what is bound in the repeater itemtemplate row (check it)
            Dim IntVariable As Integer = DataBinder.Eval(e.Item.DataItem, "value")

            'Check if value is even number
            If IntVariable Mod 2 = 0 Then
                'Get wrapper in itemtemplate that goes around each pair of dataitems
                Dim DivToColor As HtmlGenericControl = e.Item.FindControl("DivToColor")
                DivToColor.Style.Add("background-color", "green") 'change wrapper background-color
            End If
        End If
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:thamilto0410
ID: 34154587
ddayx10,  

Thank you for your response.  Exactly what I need I think.  Just need a little more help.  How would I apply if the repeater is bound to a query using the sqldatasource?  Recordset has only 1 row with about 15 columns and I am looking to check the column expedite for a value of yes which will then change the entire rows color.  Also considering you needed to do a custom write I am uping the points.  Thanks.
0
 
LVL 1

Author Comment

by:thamilto0410
ID: 34154612
Appears to me considerable effort went into this solution and we are not done yet.  Increasing points.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 1

Author Comment

by:thamilto0410
ID: 34154798
ddayx10:
Never mind.  Got it.  Was able to use what you gave above in conjunction with converting the dataitem to a DBDataRecord and then using the item property with the column name of expedite to determine if it had a yes or no.  Working great.  You are awesome.  Thanks so much.
0
 
LVL 1

Author Closing Comment

by:thamilto0410
ID: 34154815
I had to convert the above to a datasource recordset but did not take much.  The example above is excellent if you need to change row color based on a column value.
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 34160628
Glad it helped. Sorry I didn't get back until now, but seems like you handled it fine.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question