Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populating a Repeater with a DataReader/DataSet

Posted on 2004-08-12
5
Medium Priority
?
754 Views
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
test.ReadXml(MapPath("../xmlfile1.xml"))
Repeater1.DataSource = test 'drResults
Repeater1.DataBind()

<ItemTemplate>
<tr>
<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>
</tr>
</ItemTemplate>

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?

0
Comment
Question by:kriskramer
5 Comments
 
LVL 35

Expert Comment

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

or

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

Author Comment

by:kriskramer
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.
0
 
LVL 7

Expert Comment

by:mdamico
ID: 11785793
Use the onItemDataBound event

Here is an example of the onItemDataBound use:

http://aspnet.4guysfromrolla.com/articles/090402-1.2.aspx
0
 

Author Comment

by:kriskramer
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.
0
 
LVL 8

Accepted Solution

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

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

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;
            switch(text1)
                                {
                                case "1":
                    ((Label)e.Item.FindControl("lblType")).Text = "Primary Contact";
                    break;
                                case "2":
                    ((Label)e.Item.FindControl("lblType")).Text = "Additional Contact";
                    break;
                                case "3":
                    ((Label)e.Item.FindControl("lblType")).Text = "Site Manager";
                    break;
                                default:
                    ((Label)e.Item.FindControl("lblType")).Text = "Type not found";
                    break;
                                }
      }
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
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…
Suggested Courses

916 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