繁体   English   中英

从动态创建的文本框和复选框中获取价值

[英]getting value from dynamically created textbox and checkbox

我写下面的代码来创建代码

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

我想检索一个checked属性为true的复选框的textvalue以及一个buttonclick事件的相应文本框。 问题在于查找控件及其textvalue。 任何人都可以帮忙吗?感谢Advance.dt1是datatable.for窗体应用程序

该代码在生命周期中在哪里? 它应该在Page_Load之前,以便以后获得控件的值。 你可以给它一个Id

 chk.ID = myId;

要获得价值,您可以编写如下内容

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

您是否尝试过遍历控件? 如下所示:

    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

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

创建一个新的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()中,结果仍然相同。

    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"

别忘了您必须在回发时重新创建所有动态控件

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM