Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp:image control null

Status
Not open for further replies.

wallm

Programmer
Apr 4, 2005
69
GB
Just say the DataField 'PhotoPath' was null.
How could I set the code so that it display a text string "no image"

<asp:image id="EmployeeImage"
imageurl='<%# Eval("PhotoPath") %>'
alternatetext='<%# Eval("LastName") %>'
runat="server"/>
 
On the binding event of whatever control that image is placed in, use File.Exists to check if there is an existing image. If there isn't either set the path to show a "no image found" image or add a Literal/Label control and set it's Text property.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Im slightly confused.
Here's the exact code for the image <asp:Image ID="Image2" runat="server" ImageUrl='<%# Bind("ThumbURL", "~/ProductImages/{0}") %>' />

It's within a Formview as the code shows below.

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ProductID" DataSourceID="SqlDataSource2">
<EditItemTemplate>
ProductID:
<asp:Label ID="ProductIDLabel1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
ProductCode:
<asp:TextBox ID="ProductCodeTextBox" runat="server" Text='<%# Bind("ProductCode") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ProductCodeTextBox"
ErrorMessage="Please enter a product code"></asp:RequiredFieldValidator><br />
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="NameTextBox"
ErrorMessage="Please enter a product name"></asp:RequiredFieldValidator><br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DescriptionTextBox"
ErrorMessage="Please enter a product description"></asp:RequiredFieldValidator><br />
Price:
<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="PriceTextBox"
ErrorMessage="Please enter a price"></asp:RequiredFieldValidator><br />
PictureURL:
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Please upload a Picture"></asp:RequiredFieldValidator><br />
<br />
ThumbURL:
<asp:FileUpload ID="FileUpload2" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="FileUpload2"
ErrorMessage="Please enter a thumbnail"></asp:RequiredFieldValidator><br />
<%-- <asp:TextBox ID="ThumbURLTextBox" runat="server" Text='<%# Bind("ThumbURL") %>'>
</asp:TextBox>--%><br />
CatID:
<asp:TextBox ID="CatIDTextBox" runat="server" Text='<%# Bind("CatID") %>'>
</asp:TextBox><br />
Featured:
<asp:CheckBox ID="FeaturedCheckBox" runat="server" Checked='<%# Bind("Featured") %>' /><br />
Active:
<asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Bind("Active") %>' /><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
ProductCode:
<asp:TextBox ID="ProductCodeTextBox" runat="server" Text='<%# Bind("ProductCode") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="ProductCodeTextBox"
ErrorMessage="Please enter a product code"></asp:RequiredFieldValidator><br />
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="NameTextBox"
ErrorMessage="Please enter a product name"></asp:RequiredFieldValidator><br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="DescriptionTextBox"
ErrorMessage="Please enter a product description"></asp:RequiredFieldValidator><br />
<br />
Price:
<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'>
</asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="PriceTextBox"
ErrorMessage="Please enter a price"></asp:RequiredFieldValidator><br />
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="PriceTextBox"
ErrorMessage="Please enter an appropriate price" type="double" MaximumValue="1000" MinimumValue="0"></asp:RangeValidator><br />
PictureURL:
<br />
<asp:FileUpload ID="FileUpload3" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="FileUpload3"
ErrorMessage="Please select a Picture"></asp:RequiredFieldValidator><br />
ThumbURL:
<asp:FileUpload ID="FileUpload4" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="FileUpload4"
ErrorMessage="Please select a thumbnail"></asp:RequiredFieldValidator><br />
CatID:
<asp:TextBox ID="CatIDTextBox" runat="server" Text='<%# Bind("CatID") %>'>
</asp:TextBox><br />
Featured:
<asp:CheckBox ID="FeaturedCheckBox" runat="server" Checked='<%# Bind("Featured") %>' /><br />
Active:
<asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Bind("Active") %>' /><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
ProductCode:
<asp:Label ID="ProductCodeLabel" runat="server" Text='<%# Bind("ProductCode") %>'>
</asp:Label><br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>'>
</asp:Label><br />
Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Bind("Price") %>'></asp:Label><br />
PictureURL:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("PictureURL", "~/ProductImages/{0}") %>' /><br />
ThumbURL:
<asp:Image ID="Image2" OnPreRender="NewsImagePreRender" runat="server" ImageUrl='<%# Bind("ThumbURL", "~/ProductImages/{0}") %>' /><br />
CatID:
<asp:Label ID="CatIDLabel" runat="server" Text='<%# Bind("CatID") %>'></asp:Label><br />
Featured:
<asp:CheckBox ID="FeaturedCheckBox" runat="server" Checked='<%# Bind("Featured") %>' Enabled="false" /><br />
<asp:CheckBox ID="ActiveCheckBox" runat="server" Checked='<%# Bind("Active") %>' Enabled="false" /><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete">
</asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New">
</asp:LinkButton>
</ItemTemplate>
<RowStyle HorizontalAlign="Left" Width="400px" />
</asp:FormView>

thanks.
 
On the binding event of whatever control that image is placed in, use File.Exists to check if there is an existing image
So, in the FormView's "DataBinding" event, use the File.Exists method to check whether the image exists.

If there isn't either set the path to show a "no image found" image or add a Literal/Label control and set it's Text property.
If the method you used above states a file does exist, then don't do anything. If it states the file doesn't exist, checge the path of your Image control to point at an image that says "no image available" (or add a New Literal/Label control if you don't want to use an image).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,
I've made some progress with the code below.
I can reference the control with the code below and make a few changes. I was wondering how I could determine whether the datafield was null and if so set Image2.ImageUrl = "~/images/myerror.jpg"



Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound

Dim Image2 As Image

Image2 = CType(FormView1.FindControl("Image2"), Image)


If Not (Image2 Is Nothing) Then

Image2.ImageUrl = "~/images/error.jpg"
End If

End Sub

thanks
 
If you have a reference to the Image that has been bound, use it's ImageURL property along with the File.Exists method as I've suggested twice above.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top