473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 17 '05 #1
3 1341
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 17 '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 17 '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 17 '05 #4

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

Similar topics

1
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
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
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
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
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
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
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.