Solved

How to set repeater dataitem value to string variable in codebehind

Posted on 2010-11-16
6
1,990 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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