Dynamic asp form via cs (c#) code  
Author Message
twellis





PostPosted: Fri Jul 29 20:46:08 CDT 2005 Top

ASP.Net >> 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

Web Programming252  
 
 
Karl





PostPosted: Fri Jul 29 20:46:08 CDT 2005 Top

ASP.Net >> Dynamic asp form via cs (c#) code 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!)


<EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.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
>


 
 
dpfollmer





PostPosted: Wed Aug 03 10:54:51 CDT 2005 Top

ASP.Net >> Dynamic asp form via cs (c#) code 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

 
 
Karl





PostPosted: Wed Aug 03 17:00:30 CDT 2005 Top

ASP.Net >> Dynamic asp form via cs (c#) code <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!)


<EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.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
>