如何从 asp HiddenField 获取和设置值

发布于 2024-11-16 00:18:04 字数 846 浏览 4 评论 0原文

我想向 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 Markup

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

[...]

编辑

问题是找不到控件的 ID: 错误 311 名称“IsAddingNewCauseField”未声明。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-11-23 00:18:04

我认为您需要在列表视图控件中搜索它。该控件没有设计器条目,仅在动态创建时才存在。我可能是错的,因为我在手机上无法检查。

编辑包含的代码,它将遵循这些原则。

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

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
℉絮湮 2024-11-23 00:18:04

HiddenField 值属性类型为 String。您必须在 get 中将 IsAddingNewCauseField.Value 转换为 Boolean,并在 set 访问器中调用 value.ToString()。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文