convert code behind code to use TemplateField with c# and asp.net 2.0

Hello there,
I am new with asp.net
I create a page before mainly with Response.write from the code behind. Basically I create a table and fill in the dataReader field
In the new TemplateField front I just use the <%#Eval("dataFields")%> to display data. It went fine but I run into this problem. As you can see from the Code attached, originally I have a section check if the file exist then write the images into a <td> column.
I tried to convert it into
<div style="float: left;">
       <% for (int i = 1;i< 20;i++){
                    string path = "E:\\CARETSDailypull5000\\Photographs\\" ;
                    path+= Eval("ListingKey")+"_"+i.ToString +".jpg";
                    if(File.Exists(path) {
                     Response .Write ( "<img id='photo" + i.ToString() + "' width='90' border='1' src='imageP/" + Eval ("ListingKey") .ToString()+ "_" + i.ToString() + ".jpg'>" + "<br>");                  
                       } else {
                       Response.Write ("<img width='90' border='1' src='imageP/no_photo.jpg'");
                        }
                      }
             %>                    
     </div>
Will this ever work? I got error saying
The name 'File' does not exist in the current context, but I already put
<%@ import namespace="system.IO" %> on the top?
Thanks for your help

For i = 1 To 20
   If File.Exists("E:\CARETSDaily\Photographs\" + dr("ListingKey") + "_" + i.ToString + ".jpg") Then
                        Response.Write("<img id='photo" + i.ToString + "' width='90' border='1' src='imageP/" + dream("ListingKey") + "_" + i.ToString + ".jpg'>" + "<br>")
      End If
      Next

Open in new window

LVL 1
tomcattyyAsked:
Who is Participating?
 
GiftsonDJohnCommented:
Good work :-)
0
 
tomcattyyAuthor Commented:
I change the import to
<%@ Import Namespace="System.IO" %> then the above problem solved.
now I have another error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Guess I cannot use Eval() this way? Thanks
0
 
GiftsonDJohnCommented:
Are you using any databound control? such as datagrid, gridview, listview etc...
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
tomcattyyAuthor Commented:
Yes, I use a gridView with TemplateField. So most of the fields are in this form <%# Eval("fieldName")%>, But inside of this <%   %> block,I cannot use Eval() or Bind().
Thanks for helping
0
 
GiftsonDJohnCommented:
Can you post the full code?
0
 
tomcattyyAuthor Commented:
I change the aspx a little so I can display the picture at the bottom, but eventually I need to display them together with the data.

aspx
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowDetails.aspx.cs" Inherits="ShowDetails" %>
<%@ Import Namespace="System.IO" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID ="detail" runat ="server" AutoGenerateColumns ="false" 
     DataKeyNames ="ListingKey" DataSourceID="SqlDataSource1">
     <Columns >
     <asp:TemplateField>
        <ItemTemplate>
            
                <div class="listing_addr">
                <%#Eval("FullStreetAddress")+" " %><%#Eval ("City")+" "+Eval ("State")+" "+ Eval ("ZipCode")%>
                </div>                                             
                <div class="listing_top">
                
                    <div style="float: left;">
                    <img id="prop_img" width="130" src="<%= GetPath() %>" />
                    </div>
                    <div style="float: left;">
                        <div>For Sale:$ <span ><%# String.Format("{0:$####,####,##0}", Eval("ListPrice"))%></span></div> 
                        <div>Listing Date:<span><%# GetDate ( Eval("ListingDate").ToString()) %></span></div> </br>                        
                        <div><%#Eval("BedroomsTotal") %>Beds / <%#Eval("BathsTotal") %>Baths / <%#Eval("BuildingSize") %> Sqft </div>
                        <div><%#Eval("PropertySubType") %> /Built: <%#Eval("YearBuilt") %> </div>
                    </div>
                    <div style="float: left;">
                    <input id="birdy" type="image" height="25" src="img/birdsview.png" value="" "/>
                    <input id="saving" type="image" value="95143272825" src="img/icon_save.png"/>
                    save
                    </div>
                    <div style="float: left;">
                    <div>
                    </div>
                    </div>
                 </div>
                 <div class="listing_desc">
                 DESCRIPTION
                 <div>
                 
                 </div>
                 </div>    
        </ItemTemplate>
     </asp:TemplateField>
     </Columns>
     </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PropertyConnectionString %>"
            SelectCommand="SELECT * FROM [resiActiveDetWin] "></asp:SqlDataSource>
     
     
    
    </div>
    <div id="bottm_img" runat ="server" />
    </form>
</body>
</html>
 
----------  code behind -----------
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
 
public partial class ShowDetails : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        string key = Request.QueryString["id"];
        SqlDataSource1.SelectCommand =
            "SELECT * FROM [resiActiveDetWin] where ListingKey="+"'"+key+"'" ;
        string cont="";
        
        for (int i = 0; i < 20; i++)
        {
            
            if (File.Exists("E:\\CARETSDailypull5000\\Photographs\\" + key.ToString() + "_"+i.ToString ()+".jpg"))
            {
                cont += "<img id='photo" + i.ToString() + "' width='87' border='1' src='imageP/" + key.ToString() + "_" + i.ToString() + ".jpg'>";
                
            }
        }
        
        bottm_img.InnerHtml = cont;
    }
    public string GetPath()
    {
        string key = Request.QueryString["id"];
        if (File.Exists("E:\\CARETSDailypull5000\\Photographs\\" + key.ToString() + "_0" + ".jpg"))
        {
            return "imageP/"+key.ToString ()+"_0.jpg";
        }
        return "images/nophoto.jpg";
    }
    public string GetDate(string sender )
    {
        string str = sender .ToString ();
        str = str.Substring(0, 8);
        return str;
    }
}

Open in new window

0
 
tomcattyyAuthor Commented:
Hello giftsonDJohn,

in the front aspx I have an input image with id"birdy", I need to dynamically changed the value of this input. But the attribute will not take <%#Eval("address")%> ,like
<input id="birdy" type="image" src="xx" value="<%#Eval("Address")  %>/ <%# Eval("City")%>" />
Thanks
0
 
GiftsonDJohnCommented:
Hi,

It can do inside a databound context. But it has to enclose withing single quotes.

Try this.

<input id="birdy" type="image" src="xx" value='<%#Eval("Address") / Eval("City")%>' />
0
 
tomcattyyAuthor Commented:
Thank you, giftsonDJohn,

It shurely worked fine. Now about the images I want to insert into the templateField, originally I place a <div id=" botttom_img" runat="server">  and try to insert innerHtml with images. I got the error message saying cannot find the bottom_img. as soon as I move it out from TemplateField, no problem to reference it and insert the innerHtml.
Is it possible to create a div tag inside of templateField and reference it from code behind?  Appreciate your help
0
 
GiftsonDJohnCommented:
can you post the example code? It will be easy for me.
0
 
tomcattyyAuthor Commented:
I got the answer, I create a function and from the within the div have something like
<div>
<%#  DisplayImage(Eval("ListingKey")) %>
</div>
make the code behind function to return all the images.
0
 
tomcattyyAuthor Commented:
Thanks and have to continue the project with more questions ahead..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.