Results 1 to 2 of 2

Thread: Whats wrong with this ..aaarrrggghhh

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Whats wrong with this ..aaarrrggghhh

    Can someone tell me whats wrong with this asp.net code?

    <asp:TemplateColumn HeaderText="ack" Visible="True">
    <ItemTemplate>
    <%# if DataSet1.FieldValue("CALLACK", Container) = true then %>
    <img align="middle" src='image1.gif'>
    <%else%>
    <img align="middle" src='iamge2.gif'>
    <%end if%>
    </ItemTemplate>
    </asp:TemplateColumn>

    I keep getting the error message :

    "Expression expected"

    Parksie

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    The <%# %> tags are for databinding, not inline asp, so whatever is inbetween those tags must resolve to a value.
    If ... Then is not an Expression that can resolve to a value hence the error, Expression Expected. There are several ways to get around this, one way is to use IIf. so your code would look like:
    Code:
    <asp:TemplateColumn HeaderText="ack" Visible="True"> 
    <ItemTemplate> 
    <%# IIf(DataSet1.FieldValue("CALLACK", Container) = True,  _
    "<img align='middle' src='image1.gif'>",  _
    "<img align='middle' src='iamge2.gif'>" ) %> 
    </ItemTemplate> 
    </asp:TemplateColumn>
    I didn't test out this code so i might have missed a quote here or there but you should be able to get the picture.

    Here's a different way I did it, like i said there are many ways to do it, just depends on what works for ya:
    VB Code:
    1. <%@ Import Namespace="System.Data" %>
    2. <%@ Import Namespace="System.Data.SqlClient" %>
    3. <script language="vb" runat="server">
    4. Sub Page_Load(ByVal sender As Object , ByVal e As System.EventArgs)
    5.     If Not Page.IsPostBack Then
    6.         bindGrid()
    7.     End If
    8. End Sub
    9.  
    10. Sub bindGrid()
    11.     Dim connString As String = "user id=sa;password=sa;database=Scratch;server=DeathAngel;"
    12.     Dim cn As New SqlConnection(connString)
    13.     Dim cmdText As String = "Select * From Customer"
    14.     Dim cmd As New SqlCommand ( cmdText , cn )
    15.     cmd.Connection.Open()
    16.     gridCustomer.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    17.     gridCustomer.DataBind()
    18. End Sub
    19.  
    20. Function DrawCallBackImage(ByVal CallBack As object) As String
    21.     Dim IsCallBack As Boolean
    22.     Dim ImageFileName As String
    23.     IsCallBack = CType(CallBack , Boolean)
    24.     If IsCallBack Then
    25.         ImageFileName = "Image1.gif"
    26.     Else
    27.         ImageFileName = "Image2.gif"
    28.     End If
    29.     Return "<img border=""0"" src=""" & ImageFileName & """/>"
    30. End Function
    31. </script>
    32. <html>
    33.     <body>
    34.         <form id="DataSetDecision" runat="server">
    35.             <asp:DataGrid
    36.                 ID="gridCustomer"
    37.                 Runat="server"
    38.                 AutoGenerateColumns="False">
    39.                 <Columns>
    40.                     <asp:BoundColumn
    41.                         DataField="CustomerId"
    42.                         HeaderText="Cust ID"/>
    43.                     <asp:TemplateColumn
    44.                         HeaderText="CallBack">
    45.                         <ItemTemplate>
    46.                             <%# DrawCallBackImage ( Container.DataItem ( "CallBack" ) ) %>
    47.                         </ItemTemplate>
    48.                     </asp:TemplateColumn>
    49.                 </Columns>
    50.             </asp:DataGrid>
    51.         </form>
    52.     </body>
    53. </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