Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to set repeater dataitem value to string variable in codebehind

Posted on 2010-11-16
6
Medium Priority
?
2,075 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 1600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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