Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to set repeater dataitem value to string variable in codebehind

Posted on 2010-11-16
6
Medium Priority
?
2,120 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
  • 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
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

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!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

571 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