Solved

How to set repeater dataitem value to string variable in codebehind

Posted on 2010-11-16
6
1,875 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 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now