Results 1 to 4 of 4

Thread: display different value based on repeater value [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    display different value based on repeater value [RESOLVED]

    I am using a repeater in asp.net to display some data. One of my fields is a 0 or 1 value. If a 0 is the value I want to display 'NO' and 1 would display 'YES'.

    Is this possible with a repeater?

    My code is below and field RA_ON is the field that returns 0 or 1 :
    VB Code:
    1. .......
    2. <ASP:Repeater runat="server" DataSource='<%# dsFullList.DefaultView %>'>
    3. <ItemTemplate>
    4. <tr>
    5.  <td><%# dsFullList.FieldValue("ID", Container) %> </td>
    6.  <td><%# dsFullList.FieldValue("Name", Container) %> </td>
    7.  <td><%# dsFullList.FieldValue("RA_ON", Container) %> </td>
    8.  <td><%# dsFullList.FieldValue("ENTRY_DT", Container) %> </td>
    9. </tr>
    10. </ItemTemplate>
    11. </ASP:Repeater>
    12. .....
    Last edited by lleemon; Jun 18th, 2004 at 02:32 PM.

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You can either call a method from within your item template for the value or you could hook into the ItemDataBound event and set the value from there.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Llemon, as Lethal suggested, you can call it from the aspx page or from your .vb code.

    I'd suggest calling it from vb code, only because it helps seperate design from function.

    VB Code:
    1. <ASP:Repeater runat="server" DataSource='<%# dsFullList.DefaultView %>'>
    2. <ItemTemplate>
    3. <tr>
    4.  <td><asp:Label runat="server" id="lblID"></asp:Label></td>
    5.  <td><asp:Label runat="server" ud="lblName"></asp:Label> </td>
    6.  <td><asp:Label runat="server id="lblRA_On"></asp:Label></td>
    7.  <td><asp:Label runat="server" id="lblENTRY_DT"></asp:Label> </td>
    8. </tr>
    9. </ItemTemplate>
    10. </ASP:Repeater>

    VB Code:
    1. Private Sub yourRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles yourRepeater.ItemDataBound
    2.         If (e.Item.ItemType = ListItemType.Item) OrElse _
    3.            (e.Item.ItemType = ListItemType.AlternatingItem) Then
    4.  
    5.             Dim dbr As System.Data.Common.DbDataRecord = CType(e.Item.DataItem, System.Data.Common.DbDataRecord)
    6.  
    7.             Dim x As Label
    8.             x = DirectCast(e.Item.Controls("lblID"), Label)
    9.             x.Text = dbr(1).ToString
    10.             x = DirectCast(e.Item.Controls(3), Label)
    11.             x.Text = dbr("Name").ToString
    12.             x = DirectCast(e.Item.Controls(5), Label)
    13.             If dbr("RA_ON") = 0 Then
    14.                    x.Text = "NO"
    15.             Else
    16.                    x.Text = "YES"
    17.             End If
    18.              x = DirectCast(e.Item.Controls(7), Label)
    19.              x.Text = dbr("ENTRY_DT")
    20.              
    21.         End If
    22.     End Sub

    Notice how you can call controls by their name or index... the index may be different depending if you have a table in your repeater or not... etc..

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Well, I ended up going away with the repeater and going with a datagrid.

    It works so I am happy.

    Thanks to all that helped.


    VB Code:
    1. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true"%>
    2. <%@ Import Namespace="System.Data.SqlClient" %>
    3. <script  runat="server">
    4. Sub Page_Load
    5. '----------------------------------------
    6. '
    7. '----------------------------------------
    8.     Dim conPubs As SqlConnection
    9.     Dim cmdSelect as SqlCommand
    10.     conPubs = New SqlConnection("Network Library=DBMSSOCN;Data Source=xx.xx.xxx.xx;Initial Catalog=mydatabase;User ID=myID;Password=myPassword")
    11.     cmdSelect = New SqlCommand("SELECT * FROM dbo.MYTABLENAME", conPubs)
    12.     conPubs.Open()
    13.     dgrdIP.DataSource = cmdSelect.ExecuteReader()
    14.     dgrdIP.DataBind()
    15.     conPubs.Close()
    16. End Sub
    17.  
    18. Function ChangeToText(input as String) as String
    19. '----------------------------------------
    20. '
    21. '----------------------------------------
    22.     'See if the number is 0
    23.     If Int32.Parse(input) = 0 Then
    24.         Return "ON"
    25.     Else
    26.         Return "OFF"
    27.     End If
    28. End Function
    29.  
    30. Function FindDateDiff(incDate as DateTime) as String
    31. '----------------------------------------
    32. '
    33. '----------------------------------------
    34.     Dim dtNow as DateTime
    35.     dtNow = DateTime.Now
    36.     Dim iHourDiff as Integer
    37.     iHourDiff= DateDiff(DateInterval.Hour, incDate, dtNow)
    38.     If iHourDiff > 0 Then
    39.         Return "<font color=""#FF0000"">offline</font>"
    40.     Else
    41.         Return "<b>online</b>"
    42.     End if
    43. End Function
    44.  
    45. </script>
    46. <html>
    47. <head>
    48. <title>IP List</title>
    49. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    50. </head>
    51. <body>
    52. <form action="" method="post" name="frmCurrentIPs" id="frmCurrentIPs"></table>
    53.   <asp:datagrid ID="dgrdIP" ShowHeader="true" AutoGenerateColumns="false" runat="server" >
    54.     <columns>
    55.     <asp:boundcolumn HeaderText="ID" DataField="ID" />
    56.     <asp:boundcolumn HeaderText="IP" DataField="IP" />
    57.     <asp:boundcolumn HeaderText="HOSTNAME" DataField="HOSTNAME" />
    58.     <asp:templatecolumn HeaderText="RA_ON">
    59.       <itemtemplate>
    60.         <table border="0">
    61.           <tr>
    62.             <td>
    63.               <%# ChangeToText(DataBinder.Eval(Container.DataItem, "RA_ON")) %>
    64.             </td>
    65.           </tr>
    66.         </table>
    67.       </itemtemplate>
    68.     </asp:templatecolumn>
    69.     <asp:boundcolumn HeaderText="ENTRY_DT" DataField="ENTRY_DT" />
    70.     <asp:templatecolumn HeaderText="Status">
    71.       <itemtemplate>
    72.         <table border="0">
    73.           <tr>
    74.             <td>
    75.               <%# FindDateDiff(DataBinder.Eval(Container.DataItem, "ENTRY_DT")) %>
    76.             </td>
    77.           </tr>
    78.         </table>
    79.       </itemtemplate>
    80.     </asp:templatecolumn>
    81.     </columns>
    82.   </asp:datagrid>
    83.   <br>
    84.   Current Server Time is:
    85.   <% Response.Write(DateTime.Now) %>
    86. </form>
    87. </body>
    88. </html>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width