473,513 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic asp form via cs (c#) code

I am currently designing a ASP.NET form for contacts which displays a
picture depending on which contact is being viewed. This code works
perfectly thus far, but I want to add an if-case for when the picture
does not exist.

In english it would be something like this:

If file exists ("../picture_id.jpg")
Then display image source (<img src='pictures/picture_id.jpg'>
Else diplay nothing (<br><br>)

How do I verify that a file exists on the web server using c#?

Here is a "cleaned" snippet from my current .cs file:
>>>>>>>>> protected Literal imageSRC;
protected override void MyPageLoad(object src, EventArgs e)
{
int picture_id = -1;

try
{
picture_id = Convert.ToInt32(Request["picture_id"]);
}
catch
{
picture_id = -1;
}

if (picture_id > 0)
// <summary> Add if image exists code here </summary>
{
imageSRC.Text = "<img src='contacts_pictures/" + picture_id +
"_Thumb.jpg'>";
}
}>>>>>>>>>>


Thanks in advance,

David

Nov 19 '05 #1
3 1012
Your "english" version is pretty close to the real version.

if (System.IO.File.Exists(PathToFile))
{
// do something
}
else
{
// do something else
}
you'll need to get the full path to the file, that is
c:\inetpub\wwwroot\xxxx or whatever it might be, so it would be something
like:

string picturePath = string.Format("/pictures/picture_{0}.jpg", pictureId);
if (System.IO.File.Exists(Server.MapPath(picturePath) )
{
img.Src = picturePath;
}
else
{
img.Src = "pictures/default.jpg";
}

notice that I'm not doing the ugly text = "<img src=....>" that you are,
but rahter I'm using an image server control and simply setting the Src
property.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
<dp*******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I am currently designing a ASP.NET form for contacts which displays a
picture depending on which contact is being viewed. This code works
perfectly thus far, but I want to add an if-case for when the picture
does not exist.

In english it would be something like this:

If file exists ("../picture_id.jpg")
Then display image source (<img src='pictures/picture_id.jpg'>
Else diplay nothing (<br><br>)

How do I verify that a file exists on the web server using c#?

Here is a "cleaned" snippet from my current .cs file:
>>>>>>>>>> protected Literal imageSRC;
protected override void MyPageLoad(object src, EventArgs e)
{
int picture_id = -1;

try
{
picture_id = Convert.ToInt32(Request["picture_id"]);
}
catch
{
picture_id = -1;
}

if (picture_id > 0)
// <summary> Add if image exists code here </summary>
{
imageSRC.Text = "<img src='contacts_pictures/" + picture_id +
"_Thumb.jpg'>";
}
}>>>>>>>>>>>


Thanks in advance,

David

Nov 19 '05 #2
When you use the img.source instead of the long way I used (<img
src=...), how do you call this in the aspx file?

I currently use something like:
<asp:Literal id="ImageSrc" runat="server"/>

Thanks,
David

Nov 19 '05 #3
<img id="someId" runat="server" />

then declare it as

Protected someId as HtmlImage 'vb.net
protected HtmlImage someId //c#

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
<dp*******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
When you use the img.source instead of the long way I used (<img
src=...), how do you call this in the aspx file?

I currently use something like:
<asp:Literal id="ImageSrc" runat="server"/>

Thanks,
David

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
17633
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
0
3485
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
3
2931
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process....
1
6309
by: Will | last post by:
Hi all. I'm learning VB.Net and am developing a WinForms app. I'm trying to make an app that I will use to scan in one or more than on image. I want to use a tabbed interface to hold each image....
6
2905
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library...
3
1798
by: Tyler Carver | last post by:
I am trying to use some dynamic controls that are built and then added to tables. The problem that I am having is the timing of when I can populate the controls and have the state remain after a...
3
3949
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
7
1882
by: Abraham Luna | last post by:
how do i stop the dynamic validators from breaking explorer if i use a dynamic validator and move to a different control it breaks explorer and i can type in the page when i'm not supposed to....
8
2002
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
2
2924
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.