views:

56

answers:

2

Hi,

I have LinkButton and HiddenField in a list view. I want to get the hidden value from HiddenField, so I can store it in Session and when a LinkButton is clicked it transfer the hidden value ( stored in Session) to another page. But I get this error message "Object reference not set to an instance of an object." Here's the function:

    Protected Sub lvTimeSheet_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvTimeSheet.ItemCommand

    Dim id As HiddenField = TryCast(e.Item.FindControl("hfTimeSheetId"), HiddenField)
    Dim myId As String = id.Value.ToString
    Session("myId") = myId
    Server.Transfer("Destination.aspx")

End Sub

The mark-up

</asp:LinkButton><asp:HiddenField ID="hfTimeSheetId1" runat="server" Value='<%# Eval("hfTimeSheetId") %>' />

Every time the LinkButton is clicked, it causes error with above error message. Thank you for any input.

+1  A: 

My guess would be that the FindControl isn't finding the hfTimeSheetId control within the row. Do you have it designated as a managed control (i.e. runat="server")?

Also, it might help if you provided the ASPX code to see how you're defining the controls.

CAbbott
I just updated the question to include the markup.
loso
I found the mistake. It's my typo in the markup ID="hfTimeSheetId1" should be ID="hfTimeSheetId". Now, everything is fine. Thank you.
loso
A: 

The FindControl is returning null, hense the exception. Try changing it to:

Dim id As HiddenField = TryCast(e.Item.FindControl("hfTimeSheetId1"), HiddenField)
Jagd