RadGrid Eval Statement

3 posts, 0 answers
  1. Nikita Yaroshevsky
    Nikita Yaroshevsky avatar
    21 posts
    Member since:
    Apr 2010

    Posted 05 Jun 2010 Link to this post

    Hi Telerik,

    I'm having trouble with Eval () function in the RadGrid. The function works properly when used like this:

    <href="productdetails.aspx?sid=<%#Eval("Stock Number") %>"> 

    The reason I am doing all this is because I want to know if the unit displayed has a image for it, if not, then I display  error image (using javascript). In order for me to read the directory i have to know the stock number, and that is why I am using Eval Expression in this fashion:

                                    <img id="imgItem1"  alt="" src="<
                            
                                    Dim pathUrlFirst as String = "" 
                                    Dim stockNumber as String = Eval("Stock Number")
                                    Dim ImagePath As String = AppConfig("ImagePath") 
                                    Dim pathLocal as String = ImagePath & "pictures\001270\" & stockNumber & "\"   
                                    Dim di As New System.IO.DirectoryInfo(pathLocal) 
                                    Dim fi As System.IO.FileInfo() = di.GetFiles() '("*.jpg") 
                                    Dim fitemp As System.IO.FileInfo 
                                    Dim Count As Integer = UBound(fi) + 1 
                                    Dim Name As String = "" 
                                     
                                    Dim pathURL as String = "pictures/001270/" + stockNumber 
                                     
                                    If fitemp.Extension.ToLower = ".jpg" OrElse fitemp.Extension = ".gif" Then 
                                    Name = fitemp.Name 
                                    pathUrlFirst = pathURL & Name 
                                    Response.Write(pathUrlFirst) 
                                    End If 
                                     
                                     %>style="width: 120px; border: 1px solid rgb(195, 195, 195);"  /> 

    It gives me "System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." error.

    What should I do in order to use the Eval statement?

    The code for entire ItemTemplate is here:
     
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowMultiRowSelection = "True"   
        AllowSorting="True" DataSourceID="SqlDataSource1" GridLines="None"  
        Width="850px" Height="100%" Skin="Telerik" PageSize="20" > 
    <MasterTableView autogeneratecolumns="False" datasourceid="SqlDataSource1"  
            PageSize="50"
        <NoRecordsTemplate> 
           <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"  
                OnCheckedChanged="ToggleRowSelection" /> 
        </NoRecordsTemplate> 
        <Columns> 
             
             
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID"  
                SortExpression="ID" UniqueName="ID" DataType="System.Int32"  
                ReadOnly="True" Visible="False" AutoPostBackOnFilter="True"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Sys2KNum" HeaderText="Sys2KNum"  
                SortExpression="Sys2KNum" UniqueName="Sys2KNum" Visible="False"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Stock Number" HeaderText="Sort by:"  
                SortExpression="Stock Number" UniqueName="Stock Number"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Year" HeaderText="Year"  
                SortExpression="Year" UniqueName="Year"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Make"  
                HeaderText="Make" SortExpression="Make"  
                UniqueName="Make"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Model" HeaderText="Model"  
                SortExpression="Model" UniqueName="Model"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Veh Type" HeaderText="Veh Type"  
                SortExpression="Veh Type" UniqueName="Veh Type"
            </telerik:GridBoundColumn>         
            <telerik:GridBoundColumn DataField="Body Style" HeaderText="Body Style"  
                SortExpression="Body Style" UniqueName="Body Style"
            </telerik:GridBoundColumn> 
            
        </Columns> 
        <ItemTemplate> 
         
        <div style="height: auto; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px;"
            <table width="95%" border="0" cellspacing="0" cellpadding="0"
                <tr style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px">  
                    <td style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                        <div style="font-size: medium; font-weight: bold; text-align: left;">&nbsp;&nbsp;<%#Eval("Year") & " " & Eval("Make") & " " & Eval("Model") & " " & Eval("Unit Trim") & " " & Eval("Fuel Type") & " " & Eval("WheelbaseCode") & " " & Eval("Custom1")%></div
                     
                    </td> 
                <tr style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                    <td style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                        <table border="0" > 
                            <tr style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                            <td style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                            <asp:CheckBox id="CheckBox1" OnCheckedChanged="ToggleRowSelection"  AutoPostBack="True"   runat="server"></asp:CheckBox>  
                            <div runat="server" visible ="false" id="myDiv"><%#Eval("Stock Number") %></div
                            <br /> 
                             
                            </td> 
                                <td  style="border-right-width: 0px; border-bottom-width:0px; border-left-width:    0px"
                                 
                           
                                <href="productdetails.aspx?sid=<%#Eval("Stock Number") %>"> 
                                 
                                    <img id="imgItem1"  alt="" src="<%  
                                    
                                    Dim pathUrlFirst as String = "" 
                                    Dim stockNumber as String = Eval("Stock Number") 
                                    Dim ImagePath As String = AppConfig("ImagePath") 
                                    Dim pathLocal as String = ImagePath & "pictures\001270\" & stockNumber & "\"   
                                    Dim di As New System.IO.DirectoryInfo(pathLocal) 
                                    Dim fi As System.IO.FileInfo() = di.GetFiles() '("*.jpg") 
                                    Dim fitemp As System.IO.FileInfo 
                                    Dim Count As Integer = UBound(fi) + 1 
                                    Dim Name As String = "" 
                                     
                                    Dim pathURL as String = "pictures/001270/" + stockNumber 
                                     
                                    If fitemp.Extension.ToLower = ".jpg" OrElse fitemp.Extension = ".gif" Then 
                                    Name = fitemp.Name 
                                    pathUrlFirst = pathURL & Name 
                                    Response.Write(pathUrlFirst) 
                                    End If 
                                     
                                  %>style="width: 120px; border: 1px solid rgb(195, 195, 195);"  /> 
                              
                              
                             
                              
                                </a> 
                                 
                                </td> 
                                <td  style=" text-align: left;  border-right-width: 0px; border-bottom-width: 0px; border-left-width:   0px"
                                <table  style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                                <!-- 
                                <tr style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                                <td colspan="4" style="border-right-width: 0px; border-bottom-width:0px; border-left-width: 0px" > 
                                <span id="lblDescription" >  
                                    <%#IIf(Eval("Sales Description") <> "", Eval("Sales Description").ToString().Substring(0, Math.Min(150, Eval("Sales Description").Length)) & "...&nbsp;&nbsp;<href=""productdetails.aspx?sid=" & Eval("Stock Number") & """>[View More]</a>", "")%> 
                                </span> 
                                </td> 
                                </tr> --> 
                                <tr style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px" > 
                                <td  width="125px" style="border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px"
                                Engine:<br /> 
                                Transmission:<br /> 
                                Wheel Drive:<br /> 
                                Stock #:<br /> 
                                </td> 
                                <td  width="100px" style="border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px"
                                <%'#Eval("Engine Model")%><br /> 
                                <%'#Eval("Transmission")%><br /> 
                                <%'#Eval("Wheel Base")%><br /> 
                                <%#eval("Stock Number") %><br /> 
                                </td> 
                                <td  width="80px" style="border-right-width: 0px; border-bottom-width:  0px; border-left-width: 0px"
                                Color:<br /> 
                                Package:<br /> 
                                VIN:<br /> 
                                Mileage:<br /> 
                                </td> 
                                <td  width="100px" style="border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px"
                                 
                                <%#Eval("Ext2 Color")%><br /> 
                                <%#Eval("Unit Trim")%><br /> 
                                <%#Eval("Primary Serial Number")%><br /> 
                                <%#IIf((Eval("Custom2") <> "" Or Eval("Odometer") = "0"), "Please Call", Eval("Odometer"))%><br /> 
                                </td> 
                                </tr>  
                                </table>   
                                 
                                <table> 
                                <tr> 
                                    <td><%#IIf(Eval("Certified") = 1, "<img src=""images/warranty.jpg""> ", "")%></td
                                </tr> 
                                </table> 
                                                       
                                </td> 
                                <td width="250px" style="border-right-width: 0px; border-bottom-width:  0px; border-left-width: 0px"
                                    <table style="border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px"
                                        <tr> 
                                            <td width="140px" style="border-right-width: 0px; border-bottom-width:  0px; border-left-width: 0px"
                                                <div align="center"
                                                <div style="font-size: large; font-weight: bold; vertical-align: top"
                                                <%#IIf(Eval("OurPrice") & 0 <> 0, FormatCurrency(Eval("OurPrice"), 2), "In Service")%> 
                                                <br /><br /><br /> 
                                                </div> 
                                                <href="productdetails.aspx?sid=<%#Eval("Stock Number") %>"><img src="images/button/viewdetails.gif" alt="" border="0" style="margin-bottom: 5px" /></a><br /> 
                                                </div> 
                                            </td> 
                                            <td width="110px" style="border-right-width: 0px; border-bottom-width:  0px; border-left-width: 0px"
                                            <div align="center"
                                    
                                                <br /><br /> 
                                                <div id="carfax" runat="server" visible="True"
                                                    <img src="images/carfax_1owner.png" alt="" border="" /> 
                                                </div> 
                                                <br /> 
                                                </div> 
                                            </td> 
                                        </tr> 
                                        <tr> 
                                        <td colspan="2" style="border-right-width: 0px; border-bottom-width:    0px; border-left-width: 0px"
                                            <div align="center"
                                                <div id="uwm_<%#Eval("Primary Serial Number")%>style="width:150px; height:54px; cursor:hand"></div> 
                                            </div> 
                                        </td> 
                                        </tr> 
                                    </table> 
                                </td> 
                            </tr> 
                        </table> 
                     </td> 
                </tr> 
                 
            </table> 
        </div> 
        </ItemTemplate>               
        <EditFormSettings> 
            <FormTemplate> 
                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"  
                    OnCheckedChanged="ToggleRowSelection" /> 
            </FormTemplate> 
        </EditFormSettings> 
        <PagerStyle Mode="NextPrevNumericAndAdvanced" Position="TopAndBottom" /> 
    </MasterTableView> 
        <ActiveItemStyle BorderStyle="None" /> 
    </telerik:RadGrid> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
        ConnectionString="<%$ ConnectionStrings:TestString %>"  
        SelectCommand="SELECT * FROM Vehicles WHERE Sys2KNum = '001270'" > 
    </asp:SqlDataSource> 
     


  2. Veli
    Admin
    Veli avatar
    2002 posts
    Member since:
    Sep 2012

    Posted 10 Jun 2010 Link to this post

    Hello Nikita,

    <% %> is not the same as in <%# Eval("Stock Number") %>. The latter is a binding expression, while the former is an inline block expression. You cannot use Eval and Bind in this way in an inline block expression.

    What you can do instead is put all that logic into a function accepting the stock number as a string:

    Public Function GetPathByStockNumber(stockNumber) As String
        Dim pathUrlFirst as String = ""
        Dim ImagePath As String = AppConfig("ImagePath")
        Dim pathLocal as String = ImagePath & "pictures\001270\" & stockNumber & "\"  
        Dim di As New System.IO.DirectoryInfo(pathLocal)
        Dim fi As System.IO.FileInfo() = di.GetFiles() '("*.jpg")
        Dim fitemp As System.IO.FileInfo
        Dim Count As Integer = UBound(fi) + 1
        Dim Name As String = ""
          
        Dim pathURL as String = "pictures/001270/" + stockNumber
          
        If fitemp.Extension.ToLower = ".jpg" OrElse fitemp.Extension = ".gif" Then
            Name = fitemp.Name
            pathUrlFirst = pathURL & Name
            Return pathUrlFirst
        End If
         
        Return String.Empty
    End Function

    Now you can call GetPathByStockNumber() from a binding expression:

    <img id="imgItem1"  alt="" src='<%# GetPathByStockNumber(Eval("Stock Number").ToString()) %>' />


    Best wishes,
    Veli
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.

  3. Nikita Yaroshevsky
    Nikita Yaroshevsky avatar
    21 posts
    Member since:
    Apr 2010

    Posted 10 Jun 2010 Link to this post

    Thank You guys! 

Back to Top