【问题标题】:getting value from dynamically created textbox and checkbox从动态创建的文本框和复选框中获取价值
【发布时间】:2011-06-20 12:23:44
【问题描述】:

我编写以下代码来创建代码

Dim i, x, y As Integer
    x = 30
    y = 25
    i = 0
    For i = 0 To dt1.Rows.Count - 1
        Dim chk As New CheckBox()
        chk.Text = dt1.Rows(i)(0)
        chk.Location = New Point(x, y)
        chk.Font = fnt
        chk.Width = 450
        chk.ForeColor = Color.White

        Me.Panel1.Controls.Add(chk)
        chk.Name = "chk" & Convert.ToString(i)
        Dim txt As New TextBox
        txt.Location = New Point(x, y + 23)
        txt.Font = fnt
        txt.Multiline = True
        txt.Height = 46
        txt.Width = 400
        Me.Panel1.Controls.Add(txt)
        txt.Name = "txt" & Convert.ToString(i)
        y = y + 69

我想在 buttonclick 事件中检索其选中属性为 true 的复选框的文本值和相应的文本框。问题在于找到控件及其文本值。 任何人都可以帮忙吗?谢谢 Advance.dt1 是数据表。对于窗口窗体应用程序

【问题讨论】:

    标签: .net asp.net vb.net


    【解决方案1】:

    创建一个新的 VB Windows 窗体应用程序并添加一个按钮,然后将窗体的代码替换为以下代码:

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim txt As New TextBox
            txt.Name = "myText"
            txt.Left = Me.Width / 2
            txt.Top = Me.Height / 2
            txt.Text = "here is my text"
    
            Me.Controls.Add(txt)              'This will add the dynamically created object
    
            Dim anotherObj As TextBox = Me.Controls.Item("myText")    'because we know the name of the object we created before, we can retreive it back
    
            MsgBox(anotherObj.Text)      'and we can also get the text we assigned earlier.
    
    
        End Sub
    
    
    End Class
    

    最后两行可以放在另一个 Sub() 中,你仍然会得到相同的结果。

    【讨论】:

      【解决方案2】:

      该代码在生命周期中的什么位置?它应该在 Page_Load 之前,以便稍后获取控件值。你可以给它一个像

       chk.ID = myId;
      

      要获得价值,你可以写这样的东西

       CheckBox cb  =(CheckBox)Page.FindControl(myId);
      

      【讨论】:

      • 这是用于窗体应用程序的,我们为控件分配名称,并在我的代码中分配 chk.Name = "chk" & Convert.ToString(i)
      • 嗯嗯,所以 Page 将无法工作。抱歉,我以为是网络表单。
      【解决方案3】:

      您是否尝试过循环访问控件?如下所示:

          Dim ctrlName As String = String.Empty
          For Each ctrl As Control In Me.Controls
              If TypeOf ctrl Is CheckBox Then
                  Dim chk As CheckBox = CType(ctrl, CheckBox)
      
                  If chk.Checked Then
                      ctrlName = chk.Name.Replace("chk", "txt")
                  End If
              ElseIf TypeOf ctrl Is TextBox AndAlso ctrl.Name = ctrlName Then
                  Dim txt As TextBox = CType(ctrl, TextBox)
                  Dim val As String = txt.Text
                  ctrlName = String.Empty
              End If
          Next
      

      我没有测试过,只是一个想法。

      【讨论】:

        【解决方案4】:

        别忘了你必须re-create all dynamic controls on postback

        您的Page 只是一个记住类,它会在每个请求中实例化一次,如果它不重新创建这些控件以及回发请求上的关联处理程序,那么您将不会发生任何事情..

        您需要在Page_Load 之前重新创建这些控件,您可以在Page_Init 中执行此操作或覆盖CreateChildControls 方法。

        【讨论】:

          【解决方案5】:
              Dim txtbranchname1 As TextBox
              Private boxes(1) As TextBox
          
              newbox = New TextBox
              newbox.Size = New Drawing.Size(100, 20)
              newbox.Name = "txtBranchName"
              newbox.TabIndex = 1
              newbox.Dock = DockStyle.Fill
              AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
              AddHandler newbox.KeyDown, AddressOf TextBox_Keydown
              Me.TableLayoutPanel1.Controls.Add(newbox)
              txtbranchname1 = Me.TableLayoutPanel1.Controls.Item("txtBranchName")
          
              'Enter the value to textbox on runtime
              MsgBox( txtbranchname1.Text)
          
              'assign value to textbox
              txtbranchname1.Text = "Something"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-02
            • 2017-01-12
            • 2017-07-13
            相关资源
            最近更新 更多