Hello Friends,

Now a days i m  working in one project where i need to add textbox at run time when user will press button add new field then one textbox will add on the page with validation control for same textbox after user will fill those new added textboxes and press save i want to all the new added textboxes values run time and save in the database.

i m giving you code where not just textbox but you can find any web control in one page.

First add below code in the aspx page.

<asp:Table ID=”tblText” runat=”server”>
</asp:Table>

then add this code on the button click

Sub AddControlsForTextbox(ByVal name As String)
Dim newrow As New TableRow
Dim newcell As New TableCell
Dim newtextbox As New TextBox
Dim req1 As New RequiredFieldValidator
newtextbox.ID = “txt” & name
req1.ControlToValidate = “txt” & name
req1.ErrorMessage = ” Please Enter Value In  ” & name
req1.Display = ValidatorDisplay.Dynamic
Dim lbl As New Label
lbl.Text = name & ” :  ”

newcell.Controls.Add(lbl)
newcell.Controls.Add(newtextbox)
newcell.Controls.Add(req1)
newrow.Cells.Add(newcell)

tblChar.Rows.Add(newrow)

End Sub

then when user click on save button get the textbox values for that you have to get all the textboxes which is currently appearing on the page.

please use below code for above condition

Private Sub processTextbox()
For Each ctrl As Control In Page.Controls
If TypeOf ctrl Is TextBox Then
‘do something here
Else
‘do something here
End If
Next
End Sub

Enjoy the coding

same way i check textbox in one page you can check for any control just replace the textbox to any other control.

please let me know if you have any kind of problem in this code.

Tags: ,

This entry was posted on Monday, July 20th, 2009 at 5:09 am and is filed under .Net Emperor. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment