How to get and set a value to and from an asp HiddenField

I want to add a Boolean value to a HiddenField ASP.NET controller so that the value can be retained after postback. Here is my code:

Public Property IsAddingNewCause() As Boolean
    Get 
        Return IsAddingNewCauseField.value
    End Get
    Set(ByVal value As Boolean)
        IsAddingNewCauseField.value = value          
       '^^^^^^^^^^^^^^^^^^^^^this is where I want to set the HiddenField's value!
    End Set
End Property

ASP.NET Markup

<asp:ListView ID="lstNewCause" runat="server" DataSource="..." >
    <EditItemTemplate>
                <tr>
                    <td>
                        <asp:HiddenField ID="IsAddingNewCauseField" runat="server" Value="" />

[...]

EDIT

The problem is that the control's ID is not being found: Error 311 Name 'IsAddingNewCauseField' is not declared.

Upvotes: 2

Views: 6350

Answers (2)

Mike Miller
Mike Miller

Reputation: 16575

I reckons you need to search the listviews controls for it. The control doesn't get a designer entry and only exists when created dynamically. I might be wrong as I'm on my phone and can't check.

Edit included code, its going to be along these lines.

Protected Sub lstNewCause_ItemUpdated(sender As Object, e As ListViewUpdatedEventArgs)

    Dim l As ListView = DirectCast(sender, ListView)

    Dim item As ListViewItem = lstNewCause.Items(l.SelectedIndex)

    Dim IsAddingNewCauseField As HiddenField = DirectCast(item.FindControl("IsAddingNewCauseField"), HiddenField)

    '...do your stuff


End Sub

Upvotes: 2

IUnknown
IUnknown

Reputation: 22448

HiddenField value property type is String. You must convert IsAddingNewCauseField.Value to Boolean in the get and call value.ToString() in the set accessor.

Upvotes: 0

Related Questions