Basically right, if I have a Repeater or thingie like that, I want to be able to display some fields and maybe not others depending on their content.
So one cell in the table im using is to display an image, but not every record has an image. I don't want to include a path to some blank gif file, I'd prefer to leave that field empty in the DB.
So if I have this code:
That works perfectly... provided there is actually some data in that column. But if its blank, I get a red x symbol for the image tag. So if there's data in that column, display the image, otherwise just a non breaking space.....
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Private Sub rptSales_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles rptSales.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
[b] If DirectCast(e.Item.DataItem("strImagePath"), String) = String.Empty Then[/b]
Response.Write("nothing")
Else
Dim x As New System.Web.UI.WebControls.Image
x.ImageUrl = e.Item.DataItem("strImagePath")
x.Visible = True
End If
End If
End Sub
End Class
produces
Code:
Line 110: If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
Line 111: 'Dim dbr As System.Data.Common.DbDataRecord = DirectCast(e.Item.DataItem, Data.Common.DbDataRecord)
Line 112: If DirectCast(e.Item.DataItem("strImagePath"), String) = String.Empty Then
Line 113: Response.Write("nothing")
Line 114: Else
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Server Error in '/Gaffs.ie' Application.
--------------------------------------------------------------------------------
Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 109: Private Sub rptSales_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles rptSales.ItemDataBound
Line 110: If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
Line 111: If DirectCast(e.Item.DataItem("strImagePath"), String) = String.Empty Then
Line 112: Response.Write("nothing")
Line 113: Else
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
The object's a null reference. I haven't sat down to tackle this problem for more than 5 minutes at a time over the past week. I'll give it a proper looking at tomorrow morning
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
If this is Sql Server then you may be allowing nulls in that field. If so use the t-sql function isnull when selecting data. e.g. select isnull(strImpagePath, ''), etc.
This will ensure that if it's null it will return an empty string instead. Also it's probably not a good idea to have an asp.net image control on every line unless you set enableviewstate=false. Otherwise even the invisible controls will be stored in the viewstate. Another option I recommend is to use a public function in your code behind to accept the strImpagePath value and return either an image or nothing:-
public function GetImage(imagepath as string) as string
if imagepath.length=0 then
return ""
else
return "<img src=""" & imagepath & """>"
end if
end function
You bring up a good point on not using an asp image control, a simple html img tag with a runat=server attribute will suffice.
But I disagree with your statement of using late-binding (aka implicit conversion) means of Container.DataItem to accept a image path by means of a openly public statement through the code behind. Several reasons:
1) Performance hit - while not drastic, if you had 100+ images, the binding statement as provided by using the <% %> binding tags will stifle performance on the server. There's a large degree of implicit type casting occuring here.
2) Having an openly accessible function in the assembly when it does not need to be is opening a channel for misuse, malicious or not. At worst, it should be declared Protected.
3) Incorporating <% %> tags does not foster cleanly seperating content from the code-logic. This throwsback to old style ASP coding. I think you'll agree that if you had 5 or 6 controls in one databound item in a data template control, the front side aspx looks messy as sin. Many tutorials for teaching asp.net use this method only for simplicity sake.
4) Functionality ... what if there was a textbox that needed to have a red background color when its text property was empty, and default to a '<no value>' text setting? Using code-behind binding allows for cleaner manipulation of the control:
VB Code:
If dbr("textboxText") Is Dbnull.Value Then myTextBox.BackColor=Color.Red
myTextBox.BackColor="<no value>"
End If
Plenderj simply has to test for a DbNull.Value before attempting to assign the data. Or, if he incorporates the proc change you mentioned, then test for String.Empty.
Last edited by nemaroller; Nov 7th, 2004 at 10:41 AM.
I'm gald you got it working but I would recommend you take nemaroller's points into consideration. It might be worth trying to do it in the code behind itemdatabound event instead of taking the performance hit of the method above.
Yeah I'm getting surprisingly good at this kind of thing now. I have some beautiful looking repeaters now. Took me a while to cop it.
Going to dinner in my mother's tonight and perhaps cinema, but I can send you over some code for it later k?
Basically what I do is create a template in dreamweaver or frontpage, then replace the real data with some placeholders, convert the placeholders into proper asp tags, and copy and paste that into the design view of the page
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]