Populating a Repeater with a DataReader/DataSet

Posted on 2004-08-12
Last Modified: 2007-12-19
This should probably be an easy question but I can't seem to figure it out. I'm making a series of user controls that show customer/company information, and I set up the following Dataset and repeater for testing. The XML file is a temp replacement for the actual database call, and it holds ContactType, FirstName, LastName and Phone for each contact, and I can get the information to display just fine.

Dim test As New DataSet             'for testing
Repeater1.DataSource = test 'drResults

<td><%# DataBinder.Eval(Container.DataItem, "sContactType") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "FirstName") & " " & databinder.eval(container.dataitem, "LastName") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "PhoneNumber") %></td>

The problem is, the ContactType is stored as a number, and I want to associate that number with an actual text value. Without changing how the data is retrieved, how would I change the number to the actual text value of the contact type in this situation?

Question by:kriskramer
LVL 35

Expert Comment

ID: 11785068
<td><%# String.Format("{0:s}", Container.DataItem("sContactType")) %></td>


<td><%# Container.DataItem("sContactType").ToString() %></td>

Author Comment

ID: 11785259
Actually, I'm not trying to get the number as a string, I'm trying to figure out where I can put in code to change the number to text.

For example: If i have 3 contact types, Primary Contact, Additional Contact, Site Manager. Those are saved as numbers, either 1, 2 or 3 in the database, and those numbers will be returned and displayed in the repeater. I want the text to be displayed instead, though, and I don't know where I can go to put in code to make that change.

I basically want to do a
Select Case X
Case 1: X = "Primary Contact"
Case 2: X = "Additional Contact"
Case 3: X = "Site Manager"
End Select

I just need to know where I would put this at, and what X would be.

Expert Comment

ID: 11785793
Use the onItemDataBound event

Here is an example of the onItemDataBound use:

Author Comment

ID: 11785854
I know about the onItemCreated and onItemDataBound events, but I don't know how to reference or change the actual data item. The example above uses a datalist, the .cells collection isn't available in a repeater.

Accepted Solution

daffodils earned 175 total points
ID: 11787104
Here is an idea.. change the ItemTemplate and put a Label control in place...

<asp:Label ID="lblType" runat=server Text='<%# DataBinder.EvalContainer.DataItem, "sContactType")%'></asp:Label>
<td><%# DataBinder.Eval(Container.DataItem, "FirstName") & " " & databinder.eval(container.dataitem, "LastName") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "PhoneNumber") %></td>

private void repTitle_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
      if(e.Item.FindControl("lblType") != null)
            string text1 = ((Label)e.Item.FindControl("lblType")).Text;
                                case "1":
                    ((Label)e.Item.FindControl("lblType")).Text = "Primary Contact";
                                case "2":
                    ((Label)e.Item.FindControl("lblType")).Text = "Additional Contact";
                                case "3":
                    ((Label)e.Item.FindControl("lblType")).Text = "Site Manager";
                    ((Label)e.Item.FindControl("lblType")).Text = "Type not found";

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
crm development 2 68
Achieve json result 2 65
ASP.NET Web API or ASP.NET Core MVC? 3 53 mvc input file 2 32
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

18 Experts available now in Live!

Get 1:1 Help Now