Default Picture

Discussion in 'ASP .Net Datagrid Control' started by hoz, Nov 2, 2004.

  1. hoz

    hoz Guest

    Hi ,
    lets say i have an sql table Users ( id, name ,pictureurl)
    i use datagrid to show records . my problem is , when pictureurl is null how
    can i load default.jpg picture ? binding is done on run time ( using
    DataEval.DataBinder)
    hoz, Nov 2, 2004
    #1
    1. Advertising

  2. Hi,

    Use a template column and jam in an iif() and a test for isdbnull(). If the
    item is null, you use your default image.

    Code below.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn HeaderText="StringValue">
    <ItemTemplate>
    <asp:Label id=Label1 runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.StringValue") %>'>
    </asp:Label>
    <asp:Image id="Image1" runat="server" ImageUrl='<%#
    iif(isdbnull(DataBinder.Eval(Container,
    "DataItem.StringValue")),"default.jpg", DataBinder.Eval(Container,
    "DataItem.StringValue")& ".jpg" )%>'>
    </asp:Image>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub
    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    dr.Item(1) = Nothing
    Return dt
    End Function 'CreateDataSource

    "hoz" <> wrote in message
    news:...
    > Hi ,
    > lets say i have an sql table Users ( id, name ,pictureurl)
    > i use datagrid to show records . my problem is , when pictureurl is null
    > how
    > can i load default.jpg picture ? binding is done on run time ( using
    > DataEval.DataBinder)
    >
    >
    Ken Cox [Microsoft MVP], Nov 2, 2004
    #2
    1. Advertising

  3. hoz

    hoz Guest

    thanks , I have found a different answer on asp.net news

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > Hi,
    >
    > Use a template column and jam in an iif() and a test for isdbnull(). If

    the
    > item is null, you use your default image.
    >
    > Code below.
    >
    > Let us know if this helps?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    > <asp:DataGrid id="DataGrid1" runat="server"

    AutoGenerateColumns="False">
    > <Columns>
    > <asp:TemplateColumn HeaderText="StringValue">
    > <ItemTemplate>
    > <asp:Label id=Label1 runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.StringValue") %>'>
    > </asp:Label>
    > <asp:Image id="Image1" runat="server" ImageUrl='<%#
    > iif(isdbnull(DataBinder.Eval(Container,
    > "DataItem.StringValue")),"default.jpg", DataBinder.Eval(Container,
    > "DataItem.StringValue")& ".jpg" )%>'>
    > </asp:Image>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > If Not IsPostBack Then
    > DataGrid1.DataSource = CreateDataSource()
    > DataGrid1.DataBind()
    > End If
    > End Sub
    > Function CreateDataSource() As DataTable
    > Dim dt As New DataTable
    > Dim dr As DataRow
    > dt.Columns.Add(New DataColumn _
    > ("IntegerValue", GetType(Int32)))
    > dt.Columns.Add(New DataColumn _
    > ("StringValue", GetType(String)))
    > dt.Columns.Add(New DataColumn _
    > ("CurrencyValue", GetType(Double)))
    > dt.Columns.Add(New DataColumn _
    > ("Boolean", GetType(Boolean)))
    > Dim i As Integer
    > For i = 0 To 8
    > dr = dt.NewRow()
    > dr(0) = i
    > dr(1) = "Item " + i.ToString()
    > dr(2) = 1.23 * (i + 1)
    > dr(3) = (i = 4)
    > dt.Rows.Add(dr)
    > Next i
    > dr.Item(1) = Nothing
    > Return dt
    > End Function 'CreateDataSource
    >
    > "hoz" <> wrote in message
    > news:...
    > > Hi ,
    > > lets say i have an sql table Users ( id, name ,pictureurl)
    > > i use datagrid to show records . my problem is , when pictureurl is null
    > > how
    > > can i load default.jpg picture ? binding is done on run time ( using
    > > DataEval.DataBinder)
    > >
    > >

    >
    hoz, Nov 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hoz

    Default Picture

    hoz, Nov 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    365
  2. micky

    default.mspx or default.do

    micky, Dec 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    478
    Andrew Robinson
    Dec 17, 2005
  3. Gaetan
    Replies:
    3
    Views:
    1,410
    Juan T. Llibre
    Feb 14, 2006
  4. C Gillespie
    Replies:
    3
    Views:
    396
    Peter Hansen
    Mar 22, 2005
  5. Replies:
    8
    Views:
    1,225
    Mick White
    Apr 19, 2005
Loading...

Share This Page