即使清除了 Control,以编程方式创建的 ASP.NET TextBox 在回发后仍保留 Text 值

发布于 2024-10-13 03:15:19 字数 2222 浏览 2 评论 0 原文

我有一个下拉菜单,根据选择的项目,我调用 Web 服务,然后动态创建一些文本框。

我第一次下拉菜单并选择一个项目时,它工作得很好,并且文本框是动态创建和填充的。但是,下次我下拉菜单时(第一次回发后)并选择不同的内容...第二次回发后,原始值保留在文本框中。

我正在清除占位符中的所有文本框,然后重新创建它们,然后设置新值,它们如何保留旧值......特别是如果我控制并从页面中清除它们?

注意:第二次创建它们时,文本框 ID 最终会相同。这会不会有什么关系呢?需要支持此重复 ID 功能。

从 Page_Load 调用的我的代码如下:(编辑以添加更多代码)

Private Sub RefreshEntity()

        Dim XmlRecords As New XmlDocument
        Dim XmlRecordsNode As XmlNode
        Dim EntityType As String = EntityTypes.SelectedValue
        Dim Entity As String = RecordValue.Value
        Dim FieldName As String
        Dim FieldValue As String

        FieldPlaceHolder.Controls.Clear()

        If RecordList.SelectedValue <> "Select..." Then
            Try
                XmlRecordsNode = LoginInfo.SharePointConnectWebService.GetMetaData(LoginInfo.WSUser, LoginInfo.WSPass, _
                                                                                  EntityType, Entity)
                XmlRecords.LoadXml(XmlRecordsNode.OuterXml)
            Catch ex As Exception
                ConfirmLabel.Text = "<b>Error:</b><br>" & ex.Message.ToString
                Return
            End Try
        Else
            SetProperties.Visible = False
            Return
        End If

        For Each OneNode As XmlNode In XmlRecords.SelectNodes("Fields").Item(0).ChildNodes

            FieldName = OneNode.Name
            FieldValue = OneNode.InnerText

            Dim newLabel As Label = New Label()
            newLabel.Text = FieldName & ":   "

            Dim newTextBox As TextBox = New TextBox()
            newTextBox.ID = "Field-" & FieldName
            newTextBox.Text = FieldValue

            Dim newLine As Label = New Label()
            newLine.Text = "<br><br>"

            FieldPlaceHolder.Controls.Add(newLabel)
            FieldPlaceHolder.Controls.Add(newTextBox)
            FieldPlaceHolder.Controls.Add(newLine)

        Next

        SetProperties.Visible = True

    End Sub

RecordValue.Value 是一个隐藏字段,会在每个 Page_Load 中填充:

                RecordValue.Value = RecordList.SelectedValue

其中 RecordList 是我的 DropDown 菜单。

I have a drop down menu, and based on which item is selected, I call a web service and then dynamically create some text boxes.

The first time I drop down the menu and select an item, it works perfectly, and the text boxes are created and populated dynamically. However, the next time I drop down the menu (after the first postback), and select something different... after the second postback, the original values remain in the textboxes.

I am clearing all of the text boxes out of the placeholder, then re-creating them, and then setting a NEW value, how can they retain the OLD values... especially if I controls.clear them from the page?

Note: The second time they are being created, the textbox IDs DO end up being the same. Could that have something to do with it? This duplicate ID functionality will need to be supported.

My code, called from Page_Load, is as follows: (edited to add more code)

Private Sub RefreshEntity()

        Dim XmlRecords As New XmlDocument
        Dim XmlRecordsNode As XmlNode
        Dim EntityType As String = EntityTypes.SelectedValue
        Dim Entity As String = RecordValue.Value
        Dim FieldName As String
        Dim FieldValue As String

        FieldPlaceHolder.Controls.Clear()

        If RecordList.SelectedValue <> "Select..." Then
            Try
                XmlRecordsNode = LoginInfo.SharePointConnectWebService.GetMetaData(LoginInfo.WSUser, LoginInfo.WSPass, _
                                                                                  EntityType, Entity)
                XmlRecords.LoadXml(XmlRecordsNode.OuterXml)
            Catch ex As Exception
                ConfirmLabel.Text = "<b>Error:</b><br>" & ex.Message.ToString
                Return
            End Try
        Else
            SetProperties.Visible = False
            Return
        End If

        For Each OneNode As XmlNode In XmlRecords.SelectNodes("Fields").Item(0).ChildNodes

            FieldName = OneNode.Name
            FieldValue = OneNode.InnerText

            Dim newLabel As Label = New Label()
            newLabel.Text = FieldName & ":   "

            Dim newTextBox As TextBox = New TextBox()
            newTextBox.ID = "Field-" & FieldName
            newTextBox.Text = FieldValue

            Dim newLine As Label = New Label()
            newLine.Text = "<br><br>"

            FieldPlaceHolder.Controls.Add(newLabel)
            FieldPlaceHolder.Controls.Add(newTextBox)
            FieldPlaceHolder.Controls.Add(newLine)

        Next

        SetProperties.Visible = True

    End Sub

And the RecordValue.Value is a hidden field that gets populated in every Page_Load:

                RecordValue.Value = RecordList.SelectedValue

Where RecordList is my DropDown menu.

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

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

发布评论

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

评论(2

自找没趣 2024-10-20 03:15:19

这可能是由于 ViewState 或发布的值破坏了您的值。

一旦控件被动态添加到控件集合中,它就需要赶上所有已触发的页面生命周期事件。在回发的情况下,这意味着 ViewState 和/或发布的表单值将根据您将动态控件添加到控件集合并设置 .text 属性的顺序破坏 TextBox 上的 .text 属性。

要解决此问题,您可以通过设置 .EnableViewState 属性设置为 false 也会在设置控件的任何属性之前将控件添加到控件集合中。

For Each OneNode As XmlNode In XmlRecords.SelectNodes("Fields").Item(0).ChildNodes

    FieldName = OneNode.Name
    FieldValue = OneNode.InnerText

    Dim newLabel As Label = New Label()
    Dim newTextBox As TextBox = New TextBox()
    Dim newLine As Label = New Label()

    newTextBox.ID = "Field-" & FieldName

    newLabel.EnableViewState = False
    newTextBox.EnableViewState = False
    newLine.EnableViewState = False

    FieldPlaceHolder.Controls.Add(newLabel)
    FieldPlaceHolder.Controls.Add(newTextBox)
    FieldPlaceHolder.Controls.Add(newLine)

    newLabel.Text = FieldName & ":   "
    newTextBox.Text = FieldValue
    newLine.Text = "<br><br>"

Next

This is likely due to ViewState or the Posted values clobbering your values.

Once a control is dynamically added to the controls collection it needs to catch up with all the page life cycle events that have already fired. In the case of a post back this means that the ViewState and/or the posted form value will clobber the .text property on the TextBox based on the order you're adding the dynamic controls to the controls collection and setting the .text property.

To fix this you can disable ViewState by setting the .EnableViewState property to false on the dynamically generate controls also add the controls to the controls collection before you set any properties on them.

For Each OneNode As XmlNode In XmlRecords.SelectNodes("Fields").Item(0).ChildNodes

    FieldName = OneNode.Name
    FieldValue = OneNode.InnerText

    Dim newLabel As Label = New Label()
    Dim newTextBox As TextBox = New TextBox()
    Dim newLine As Label = New Label()

    newTextBox.ID = "Field-" & FieldName

    newLabel.EnableViewState = False
    newTextBox.EnableViewState = False
    newLine.EnableViewState = False

    FieldPlaceHolder.Controls.Add(newLabel)
    FieldPlaceHolder.Controls.Add(newTextBox)
    FieldPlaceHolder.Controls.Add(newLine)

    newLabel.Text = FieldName & ":   "
    newTextBox.Text = FieldValue
    newLine.Text = "<br><br>"

Next
清眉祭 2024-10-20 03:15:19

您不会将该值存储在会话变量中,然后稍后在代码中将其放回到文本框中吗?

You're not storing the value in a Session variable and then putting it back into the text box later in your code?

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