如何从asp HiddenField获取和设置值

时间:2011-06-15 18:44:51

标签: asp.net vb.net boolean hiddenfield

我想向HiddenField ASP.NET控制器添加一个布尔值,以便在回发后保留该值。这是我的代码:

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标记

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

[...]

修改

问题是找不到控件的ID: Error 311 Name 'IsAddingNewCauseField' is not declared.

2 个答案:

答案 0 :(得分:2)

我估计你需要搜索listviews控件。控件不会获得设计器条目,只有在动态创建时才存在。我可能错了,因为我在手机上无法检查。

编辑包含的代码,它将沿着这些行。

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

答案 1 :(得分:0)

HiddenField值属性类型是String。您必须在设置访问器中的get和call value.ToString()中将IsAddingNewCauseField.Value转换为Boolean。