getting value from dynamically created textbox and checkbox
i write the following code to create code
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
i want to retrive the textvalue of checkbox whose checked property is true and respective textbox at a buttonclick event. Problem is in finding the controls and their textvalue. any one can help?Thanks in Advance.dt1 is datatable .For window form application
Create a new VB Windows Form application and add a button then replace the code of the form with this code:
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
The last 2 lines can be put inside another Sub() and you would still have the same result.
Where is that code in the life cycle? It should be before Page_Load in order to get the controls values later. You can give it an Id like
chk.ID = myId;
To get the value you can write something like this
CheckBox cb =(CheckBox)Page.FindControl(myId);
Have you tried looping through the controls? Something like below:
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
I haven't tested this, just an idea.
Don't forget you must re-create all dynamic controls on postback
Your Page
is just a class remember and it is instantiated once per request, if it doesn't recreate these controls as well as the associated handlers on the postback request then you won't get anything happen..
You need to recreate these controls prior to Page_Load
, you can do this in Page_Init
or override the CreateChildControls
method.
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"
精彩评论