将控件添加到集合并从集合更新

发布于 2024-10-26 02:14:12 字数 543 浏览 1 评论 0原文

我有一个 ASP.NET 应用程序,页面上有很多文本框,需要在程序执行的各个点进行更新。

这些文本框实际上属于某个类,因此为了方便更新,我想我可以创建一个字典(字符串,对象)并将 control.IDcontrol 添加到然后进行更新,执行如下操作:(

在更新 textbox.text 的情况下):

for each kv as KeyValuePair(Of string, object) in mytextboxes
      if (kv.Key.Contains("textboxid")) then
             DirectCast(kv.Value, TextBox).Text = mystring
      end if
next

但是,文本框的 text 属性实际上并未更新。我主要是想避免每次更新文本框时都必须手动执行 textbox.text = somestring

这是一个可行的解决方案吗? 如果是的话,我做错了什么?

I have an ASP.NET app with lots of textboxes all over the page that need updating at various points through program execution.

These textboxes actually belong to a certain class, so for easy updating I thought I could create a Dictionary(Of string, object) and add the control.ID and the control to it and then for updating do something like this:

(in case of updating textbox.text):

for each kv as KeyValuePair(Of string, object) in mytextboxes
      if (kv.Key.Contains("textboxid")) then
             DirectCast(kv.Value, TextBox).Text = mystring
      end if
next

However the text property of the textbox does not actually get updated. I'm mainly trying to avoid having to manually do textbox.text = somestring for each one of my textboxes every time I have to update them.

Is this a feasible solution that could be made to work?
If so, what have I done wrong?

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

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

发布评论

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

评论(3

疑心病 2024-11-02 02:14:12

您的函数需要直接转换控件,而不是 KeyValuePair 集合。尝试这样的事情......

Private Sub SetTextboxText(ByVal strTextBoxID As String, ByVal strText As String)

    Dim txtChangeThisOne As TextBox = CType(Page.FindControl(strTextBoxID), TextBox)

    If Not txtChangeThisOne Is Nothing Then

        txtChangeThisOne.Text = strText

    End If

End Sub

然后打电话......

SetTextboxText("TextboxID", "Text you wish to set.")

Your function needs to cast the control directly, not a KeyValuePair collection. Try something like this...

Private Sub SetTextboxText(ByVal strTextBoxID As String, ByVal strText As String)

    Dim txtChangeThisOne As TextBox = CType(Page.FindControl(strTextBoxID), TextBox)

    If Not txtChangeThisOne Is Nothing Then

        txtChangeThisOne.Text = strText

    End If

End Sub

Then to call...

SetTextboxText("TextboxID", "Text you wish to set.")
迷路的信 2024-11-02 02:14:12

如果您必须使用字典,请尝试以下操作:

Dim mytextboxes As New Dictionary(Of String, TextBox)
Dim mystring As String = "A Input String."
Dim myTextBoxName As String = "TextBox1"
Dim t As TextBox
For Each c As Object In Me.Controls
    If (TypeOf c Is TextBox) Then
        t = CType(c, TextBox)
        mytextboxes.Add(t.Name, t)
    End If
Next

Try
    mytextboxes(myTextBoxName).Text = mystring
Catch ex As Exception
    MsgBox("There is no " & myTextBoxName)
End Try

If you have to use a dictionary, try this:

Dim mytextboxes As New Dictionary(Of String, TextBox)
Dim mystring As String = "A Input String."
Dim myTextBoxName As String = "TextBox1"
Dim t As TextBox
For Each c As Object In Me.Controls
    If (TypeOf c Is TextBox) Then
        t = CType(c, TextBox)
        mytextboxes.Add(t.Name, t)
    End If
Next

Try
    mytextboxes(myTextBoxName).Text = mystring
Catch ex As Exception
    MsgBox("There is no " & myTextBoxName)
End Try
美胚控场 2024-11-02 02:14:12

我有一个使用 100 个按钮的项目,它们共享一个事件处理程序。如果您可以在数组中创建所有文本框,您也许能够定义一个公共事件处理程序。以下片段将用作参考。希望VB代码仍然有帮助。
如果您想测试代码,您需要有一个表单和 10 X 10 的布局。如果有帮助,请告诉我。

Public Class Form1
    Private NRow As Integer = 10
    Private NCol As Integer = 10
    Private BtnArray(NRow * NCol - 1) As Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TableLayoutPanel1.Size = Me.ClientSize
        For i As Integer = 0 To BtnArray.Length - 1
            BtnArray(i) = New Button()
            BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            BtnArray(i).Text = CStr(i)
            TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod NCol, i \ NCol)
            AddHandler BtnArray(i).Click, AddressOf ClickHandler
        Next
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("I am button #" & CType(sender, Button).Text)
    End Sub
End Class

I have a project that uses 100 buttons and they share one event handler. If you could create all of the TextBoxes in an Array, you may be able to define a common event handler. The following snippit would be used as a reference. Hope the VB code is still helpful.
If you want to test the code, you need to have a Form and a Layout of 10 X 10. Let me know if it helps.

Public Class Form1
    Private NRow As Integer = 10
    Private NCol As Integer = 10
    Private BtnArray(NRow * NCol - 1) As Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TableLayoutPanel1.Size = Me.ClientSize
        For i As Integer = 0 To BtnArray.Length - 1
            BtnArray(i) = New Button()
            BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            BtnArray(i).Text = CStr(i)
            TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod NCol, i \ NCol)
            AddHandler BtnArray(i).Click, AddressOf ClickHandler
        Next
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("I am button #" & CType(sender, Button).Text)
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文