FindControl in Datalist object

A

Anne Butera

Hello,
I am trying to set an image visible field to False if the dataset is null.
With the code below, I am getting an error - "Object reference not set to an
instance of an object."

First - the code behind:
Protected Sub Image1_DataBinding(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim storePic As New Image
storePic = dlNewsItems.FindControl("image1")
If storePic.ImageUrl = "" Then

storePic.Visible = False
Else
End If
End Sub

And the Datalist:
<asp:DataList ID="dlNewsItems" runat="server" DataSourceID="sqlNewsTable" ">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#
Eval("newsPicUrl", "{0}") %>'
OnDataBinding="Image1_DataBinding"/>
</ItemTemplate>
</asp:DataList>


Any help is appreciated.

Thank you,
Anne
 
S

Stan

Hello,
I am trying to set an image visible field to False if the dataset is null. 
With the code below, I am getting an error - "Object reference not set to an
instance of an object."

First - the code behind:
 Protected Sub Image1_DataBinding(ByVal sender As Object, ByVal e As
System.EventArgs)
        Dim storePic As New Image
        storePic = dlNewsItems.FindControl("image1")
        If storePic.ImageUrl = "" Then

            storePic.Visible = False
        Else
        End If
    End Sub

And the Datalist:
<asp:DataList ID="dlNewsItems" runat="server" DataSourceID="sqlNewsTable" ">
            <asp:Image ID="Image1" runat="server" ImageUrl='<%#
Eval("newsPicUrl", "{0}") %>'
                   OnDataBinding="Image1_DataBinding"/>  
   </ItemTemplate>
 </asp:DataList>

Any help is appreciated.

Thank you,
Anne

The DataBinding event is not the best one to use. The
dtNewsItems.FindControl() method will not retrieve a control from the
item template.

Better to use the ItemDataBound event. In that routine the 'e'
parameter has an 'item' property that will give you access to the
Image control using e.item.FindControl("image1").

BTW no point in creating an Image with the new() constructor if you
are retrieving one that already exists. All you will need is:

Dim StorePic as Image = e.item.FindControl("image1")

HTH
 
N

Nathan Sokalski

Here are two things you may want to look at:

1. The DataBinding event occurs while the items are being databound,
therefore the items do not exist during that event, hence your error

2. When doing tasks like the one it looks like you are doing, you want to
use the ItemDataBound event of the DataList. To look at the value of a
specific field during this event, use the following:

CType(e.Item.DataItem,DataRowView)("newsPicUrl")

To access a specific control in the template during this event, use the
following (don't forget to convert this to an Image, or whatever the
control's type is):

e.Item.FindControl("Image1")

Hopefully all this will help you accomplish your goal. Good Luck!
 
A

Anne Butera

Hello and thank you so much for your reply.

I have modified my code, and still receive the same error: Object reference
not set to an instance of an object. Please help me with what I am doing
wrong, I have spent many hours trying to figure this out. THANK YOU so much!

Here is what I have done:


Protected Sub dlNewsItems_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataListItemEventArgs) Handles
dlNewsItems.ItemDataBound
Dim StorePic as Image = e.item.FindControl("image1")
If storePic.ImageUrl = "" Then

storePic.Visible = False
Else
End Sub

<asp:DataList ID="dlNewsItems" runat="server" DataSourceID="sqlNewsTable"
Width="95%">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#
Eval("newsPicUrl") %>' />
</ItemTemplate>

</asp:DataList>
 
S

Stan

Hello,
I am trying to set an image visible field to False if the dataset is null. 
With the code below, I am getting an error - "Object reference not set to an
instance of an object."

First - the code behind:
 Protected Sub Image1_DataBinding(ByVal sender As Object, ByVal e As
System.EventArgs)
        Dim storePic As New Image
        storePic = dlNewsItems.FindControl("image1")
        If storePic.ImageUrl = "" Then

            storePic.Visible = False
        Else
        End If
    End Sub

And the Datalist:
<asp:DataList ID="dlNewsItems" runat="server" DataSourceID="sqlNewsTable" ">
            <asp:Image ID="Image1" runat="server" ImageUrl='<%#
Eval("newsPicUrl", "{0}") %>'
                   OnDataBinding="Image1_DataBinding"/>  
   </ItemTemplate>
 </asp:DataList>

Any help is appreciated.

Thank you,
Anne

Hi Anne

You need to use the ItemDataBound event not the ItemDataBinding
event.

The ItemDataBinding event is raised before data binding commences and
before the databound template controls have been instantiated, hence
the null reference.

The ItemDataBound event occurs after the data binding has been
completed which allows an opportunity to ammend the databounds control
before they are finally rendered.
 
S

Stan

Hi Anne

You need to use the ItemDataBound event not the ItemDataBinding
event.

The ItemDataBinding event is raised before data binding commences and
before the databound template controls have been instantiated, hence
the null reference.

The ItemDataBound event occurs after the data binding has been
completed which allows an opportunity to ammend the databounds control
before they are finally rendered.- Hide quoted text -

- Show quoted text -

Dear Anne

Please ignore my last message, on re-reading your code I see you did
indeed use the ItemDataBound event. Sorry about that.

However, I can see what may the problem. Although the VB language is
not case sensitive, indexed parameter names (passed as literal
strings) probably are case sensitive.

If you look carefully, you have typed the name of the Image1 control
differently in the e.item.FindControl() method to that in the ID
attribute on the source page. Try ammending your VB code as follows:

replace

storePic = dlNewsItems.FindControl("image1")

with

storePic = dlNewsItems.FindControl("Image1")

Hope that solves it.
 
S

Stan

Hi Anne

You need to use the ItemDataBound event not the ItemDataBinding
event.

The ItemDataBinding event is raised before data binding commences and
before the databound template controls have been instantiated, hence
the null reference.

The ItemDataBound event occurs after the data binding has been
completed which allows an opportunity to ammend the databounds control
before they are finally rendered.- Hide quoted text -

- Show quoted text -

Anne

Please ignore my last message, on re-reading your code I see you did
indeed use the ItemDataBound event. Sorry about that.


However, I can see what may the problem. Although the VB language is
not case sensitive, indexed parameter names (passed as literal
strings) probably are case sensitive.


If you look carefully, you have typed the name of the Image1 control
differently in the e.item.FindControl() method to that in the ID
attribute on the source page. Try ammending your VB code as follows:

(*Another correction I should have said:*)

replace


storePic = e.Items.FindControl("image1")


with


storePic = e.Items.FindControl("Image1")


Hope that solves it.
 
A

Anne Butera

Thanks so much, but no luck. I tried changing the case but same error.
Here's my code now:

Protected Sub dlNewsItems_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs)
Dim storepic As Image = e.Item.FindControl("Image1")
storepic.Visible = False

End Sub
 
S

Stan

Thanks so much, but no luck.  I tried changing the case but same error.  
Here's my code now:

 Protected Sub dlNewsItems_ItemDataBound(ByVal sender As Object, ByVal eAs
System.Web.UI.WebControls.DataListItemEventArgs)
         Dim storepic As Image = e.Item.FindControl("Image1")
        storepic.Visible = False

    End Sub









- Show quoted text -

Dear Anne

I hope I've finally got an answer. It seems that accessing controls in
the ItemTemplate is a tad more complex than I thought.

Your code needs to be something like this:

Protected Sub dlNewsItems_ItemDataBound(ByVal sender As Object, ByVal
e As
System.Web.UI.WebControls.DataListItemEventArgs)

Dim storepic As Image
Dim drv as System.Data.DataRowView
if (e.item.Itemtype = ListItemType.Item) or (e.item.ItemType =
ListItemType.AlternatingItem) Then
storepic = e.Item.FindControl("Image1")
drv = e.item.DataItem
if drv.row.IsNull("newspicurl") Then
storepic.Visible = False
End if
End if

Apparently the ItemDataBound event can be raised when the e.item
object is something other than a row from the ItemTemplate so you have
to check for it. It is also necessary to capture those occasions when
the row index is even i.e. the AlternatingItem template.

Note that I have used the e.Item.DataItem property and tested for a
null value there rather than the Image.ImageUrl property.

I have tested the above code in a dummy project so I know it works if
all else is as it should be.

Good luck
 
A

Anne Butera

That did the trick!!! THANK YOU STAN!

Stan said:
Dear Anne

I hope I've finally got an answer. It seems that accessing controls in
the ItemTemplate is a tad more complex than I thought.

Your code needs to be something like this:

Protected Sub dlNewsItems_ItemDataBound(ByVal sender As Object, ByVal
e As
System.Web.UI.WebControls.DataListItemEventArgs)

Dim storepic As Image
Dim drv as System.Data.DataRowView
if (e.item.Itemtype = ListItemType.Item) or (e.item.ItemType =
ListItemType.AlternatingItem) Then
storepic = e.Item.FindControl("Image1")
drv = e.item.DataItem
if drv.row.IsNull("newspicurl") Then
storepic.Visible = False
End if
End if

Apparently the ItemDataBound event can be raised when the e.item
object is something other than a row from the ItemTemplate so you have
to check for it. It is also necessary to capture those occasions when
the row index is even i.e. the AlternatingItem template.

Note that I have used the e.Item.DataItem property and tested for a
null value there rather than the Image.ImageUrl property.

I have tested the above code in a dummy project so I know it works if
all else is as it should be.

Good luck
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top