[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-22
12
Medium Priority
?
1,094 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:tomcattyy
  • 7
  • 5
12 Comments
 
LVL 1

Author Comment

by:tomcattyy
ID: 24210199
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
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24212539
Are you using any databound control? such as datagrid, gridview, listview etc...
0
 
LVL 1

Author Comment

by:tomcattyy
ID: 24218226
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24218279
Can you post the full code?
0
 
LVL 1

Author Comment

by:tomcattyy
ID: 24218468
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
 
LVL 1

Author Comment

by:tomcattyy
ID: 24218620
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
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24221365
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
 
LVL 1

Author Comment

by:tomcattyy
ID: 24227286
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
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24227330
can you post the example code? It will be easy for me.
0
 
LVL 1

Author Comment

by:tomcattyy
ID: 24227376
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
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 2000 total points
ID: 24227414
Good work :-)
0
 
LVL 1

Author Comment

by:tomcattyy
ID: 24227896
Thanks and have to continue the project with more questions ahead..
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline

834 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