Using the Textbox.Text on Dynmaicly Created Textboxes in VB.Net

I found a script that i modified to create textbox fields Dynamicly.  I am now faced with the delimia of figuring out a way to pull the data in those dynamicly created fields.  Since they are not defined, I have not found a way to use a variable to pull the data.

Here is the code .aspx


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DynamicTextboxes.aspx.vb" Inherits="DynamicTextboxes" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server"> 
       <h5 style="text-align: left">
       <asp:Label ID="Label2" runat="server"></asp:Label>
     <div style="text-align: left">  
       <asp:Table runat="server" id="Quotes">
       <asp:TableHeaderCell runat="server" ID="TableCell13" Visible="false"></asp:TableHeaderCell>
       <asp:TableHeaderCell> </asp:TableHeaderCell>
       <asp:TableRow runat="server" ID="TableRow1">
       <asp:TableCell runat="server" ID="TableCell6" Visible="false"></asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell1" Wrap="false">Item 1</asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell2"><asp:TextBox ID="Name1" runat="server" Width="100"></asp:TextBox></asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell3"><asp:TextBox ID="Model1" runat="server" Width="75"></asp:TextBox></asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell4"><asp:TextBox ID="Qty1" runat="server" Width="30"></asp:TextBox></asp:TableCell>
       <asp:TableRow runat="server" ID="TableRow2">
       <asp:TableCell runat="server" ID="TableCell5" Visible="false"></asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell7" Wrap="false">Item 2</asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell8"><asp:TextBox ID="Name2" runat="server" Width="100"></asp:TextBox></asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell9"><asp:TextBox ID="Model2" runat="server" Width="75"></asp:TextBox></asp:TableCell>
       <asp:TableCell runat="server" ID="TableCell10"><asp:TextBox ID="Qty2" runat="server" Width="30"></asp:TextBox></asp:TableCell>
           <asp:Button ID="Button1" runat="server" Text="Add Row" />
           <asp:Button ID="Submit1" runat="server" Text="Submit" OnClick="Submit"/>

Here is the code .aspx.vb


Partial Class DynamicTextboxes
    Inherits System.Web.UI.Page

    Private _additionalRowCount As Integer = 0

    Private _previousPrinterName As Integer = -1 ' required initial value
    Private _previousPrinterModel As Integer = -1 ' required initial value
    Private _previousQty As Integer = -1 ' required initial value

    Private targetTable As Table = Nothing 'assigned in PreInit

    Private ReadOnly Property NextNameTextboxID() As String

            Dim PrinterNameString As String = "PrinterName{0}"

            If Me._previousPrinterName = -1 Then
                Me._previousPrinterName = (targetTable.Rows.Count - 1)
            End If

            Dim nextPrinterNameTBId As String = String.Format(PrinterNameString, (Me._previousPrinterName + 1).ToString())
            Me._previousPrinterName += 1

            Return nextPrinterNameTBId

        End Get
    End Property
    Private ReadOnly Property NextModelTextboxID() As String
            Dim PrinterModelString As String = "PrinterModel{0}"

            If Me._previousPrinterModel = -1 Then
                Me._previousPrinterModel = (targetTable.Rows.Count - 1)
            End If

            Dim nextPrinterModelTBId As String = String.Format(PrinterModelString, (Me._previousPrinterModel + 1).ToString())
            Me._previousPrinterModel += 1

            Return nextPrinterModelTBId

        End Get
    End Property
    Private ReadOnly Property NextQtyTextboxID() As String
            Dim QtyString As String = "Qty{0}"

            If Me._previousQty = -1 Then
                Me._previousQty = (targetTable.Rows.Count - 1)
            End If

            Dim nextQtyTBId As String = String.Format(QtyString, (Me._previousQty + 1).ToString())
            Me._previousQty += 1

            Return nextQtyTBId
        End Get
    End Property
     Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

        ' initialise reference variable
        ' ****************************************************************************************
        Me.targetTable = Me.Quotes ' set to appropriate table instance (myTable etc)!
        ' ****************************************************************************************

        Integer.TryParse(Request.Form("additionalRows"), _additionalRowCount)

        If _additionalRowCount > 0 Then
            ' replace / restore dynamic rows
            Dim i As Integer = 0
            While i < _additionalRowCount
                Me.AddAdditonalRow(False) 'false because we are recreating and don't want the row count increased
                i += 1
            End While

        End If

    End Sub
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        ' embed row count in output form as hidden field.
        ' Viewstate is not available in PreInit and this is the only reliable way to proceed
        ' you could provide a multi part value with a hash or an encrypted value. 
        ' This persistence model is arbitrary but it must be persisted in the Form 
        ' as a posted value with a known client side element ID (name)
        ClientScript.RegisterHiddenField("additionalRows", _additionalRowCount.ToString())

    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim tr As TableRow = Me.AddAdditonalRow(True) ' increase row count

    End Sub
    Private Sub FormatItem(ByRef tr As TableRow)

        ' you would format the TableRow cells in this method, 
        ' possibly passing another object param to assign values from
        ' set some of the controls in the row
        tr.Cells(0).Text = Server.HtmlEncode(String.Format("Item {0}", (targetTable.Rows.Count.ToString() - 1)))

    End Sub
    Sub Submit(ByVal sender As Object, ByVal e As EventArgs)

        Dim TableView As String

        TableView = "<table><tr><td>&nbsp;</td><td style='height: 26px;'>" _
        + "Name:</td><td>Model:</td><td>Qty:</td></tr><tr>" _
        + "<td nowrap='nowrap' style='height: 26px;'>Item 1" _
        + "</td><td>" & Name1.Text _
        + "</td><td>" & Model1.Text _
        + "</td><td>" & Qty1.Text  + "</td></tr>"

        Dim X As Integer
        X = 2
        Do While X <= 5
            If Name2.Text = "" Then
                TableView = TableView & "</table>"
                TableView = TableView & "<tr><td nowrap='nowrap' style='height: 26px;'>Item 2" _
                + "</td><td>" & Name2.Text _
                + "</td><td>" & Model2.Text _
                + "</td><td>" & Qty2.Text + "</td></tr>"
            End If
            X += 1
        '' Loop doesn't work correctly because i am using the static Name2.Text, instead of the dynamic textbox "Name" & X & ".Text"
        TableView = TableView + "</table>" 'adding this line until Loop is working correctly

        Label2.Text = TableView.ToString

    End Sub
    Private Function AddAdditonalRow(ByVal incrementcounter As Boolean) As TableRow


        Dim tr As New TableRow
        Dim cell1, cell2, cell3, cell4, cell5, cell6 As New TableCell

        cell1.Wrap = False

        Dim name As New TextBox
        name.Width = 100
        Dim model As New TextBox
        model.Width = 75
        Dim qty As New TextBox
        qty.Width = 30

        name.ID = Me.NextNameTextboxID

        model.ID = Me.NextModelTextboxID

        qty.ID = Me.NextQtyTextboxID


        If incrementcounter Then
            _additionalRowCount += 1
        End If

        Return tr

    End Function
End Class

8/17/2007 2:07:01 PM 93655 articles. 6 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 46


i guess no.of of text boxes you have is equal to is targetTable.Rows.Count.

at server side you can find a control if you know id of control using Page.FindControl() function.


so you can use loop and change number and access textboxes like PrinterName1, PrinterName2, PrinterNam3.



Don't forget to click "Mark as Answer" on the post that helped you.
8/17/2007 2:49:02 PM

Try this :

Response.Write("TextBox Value: " & (CType(FindControl("controlnamecomeshere"), TextBox)).Text)


Suprotim Agarwal

ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
8/17/2007 3:15:30 PM

I have used the replies above to create this code - thanks for your help....

But I still have an issue, if the textbox doesn't exist.... can anyone tell me how to change the

If nameString = "" Then

to something like

If nameString exists??  It maybe a stilly question, but I have searched and haven't found a solution.  Any assistance is greatly appreciated!


Dim X As Integer
        X = 2

        Do While X <= 5

            Dim nameString As String = CType(FindControl("Name" & X), TextBox).Text
            Dim modelString As String = CType(FindControl("Model" & X), TextBox).Text
            Dim qtyString As String = CType(FindControl("qty" & X), TextBox).Text
            Dim item As String = "Item" & X

            If nameString = "" Then
                TableView = TableView
                TableView = TableView & "<tr><td nowrap='nowrap' style='height: 26px;'>" & item _
                + "</td><td>" & nameString _
                + "</td><td>" & modelString _
                + "</td><td>" & qtyString + "</td></tr>"
            End If
            X += 1


8/18/2007 5:59:31 PM
I figured it out!!!!! 

Do While (targetTable.Rows.Count - 1) >= X

I had to use the -1 cuz I have header rows, but it works great!!!!!!!



8/18/2007 6:12:42 PM
I figured it out!!!!! 

Do While (targetTable.Rows.Count - 1) >= X

I had to use the -1 cuz I have header rows, but it works great!!!!!!!


8/18/2007 6:12:49 PM

Sorry about the duplicate post.

 I have seen others do the same thing, the regular reply doesn't seem to be a problem.  At least on my computer... It was the quick reply that caused the duplicates.  It didn't appear to post, so I hit it again.  Then just closed out, because it didn't appear to be working.


8/18/2007 6:15:26 PM

Similar Artilces:

Sending SMS using or using or
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

hei guys, i need help on my application, i'm using C#.NET as front-ed and SQL as back-end, aside from a datagrid, i want to display my data using textboxes and not a datagrid, i can do it using Visual Basic 6 but since i'm a newbee at C#.NET i don't know how to do it, specially the codes, can anyone help me with it? thank you in advance! If you are doing windows development, i would suggest using 'DataForm wizard' (Use Solution Explorer-->Rightclick project-->Add new Item-->New DataForm). It will guide you through steps. If you are doing web apps. Use quickstart to learn them. h...

.Net 2.0 Web application using is unable to create object of another dll writen in C# .net 2.0
Hi, I habe created one web application using which is adding reference of dlls written in C# and .net 2.0. But whenever trying to create object of referencing dll, it is throwing error :Object reference does not set to an object. But, locally it is working fine,. In the server i have deployed the .aspx files and dll files in the bin. There were already an web.config in the server which is of .net 1.1. But checked it is retrieving value for that web.config correctly. Should i have to deply any other files and if not what can be the solution for this? Please help. Thanks, So...

Highlighting(chaning the color) the part of the text in textbox using javascript or
Hi Frnds, Please can u help me how to Highlighting(chaning the color) the part of the text in textbox  using javascript or suppose in my text box some words like HAPPY,UNHAPPY,...I have to find those words and highlight measn changing the background color of  those words.. How to do this using javascript or please can u help me out.. ThanksSandhya I think you will need to use DIV with runat="server". then you can easily insert html text in it. You may use it in following wayDim t As String = "Its really working! I am HAPPY"t = t.Replace(&qu...

Create array Textbox (
How I create Array Component ( Like this : Textbox1(0).text="First" Textbox1(1).text="Second" can it create by design. Thanx Regards check out below example > VB.NET: What Happened to Control Arrays!!! (Part I) WinForms Controls—Creating Control Arrays in VB.NEThope it helps./. Thanx, [KaushaL] || BloG || Profile || Microsoft MVP"I would love to change the world, but they won’t give me the source code"Don't forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and mark your thr...

Creating a web form in VB.NET 2003
I apologise if this has been asked a thousand times.  Firstly, I am using VB.NET 2003 (v7.1.3088) and .NET Framework 1.1.4322  SP1 on WinXP Professional SP2 platform with all security and critical updates in place.My background: good old FoxPro, VB6 and VBA under Access (yuk!)I am playing about with a test project before hopefully upgrading to VB.NET 2005 and i am having a fundamental problem with web forms for a ASP.NET web application.  Whenever I copy and paste an object (text box, label, etc) VB.NET pastes the object back onto the form apparently correctly.  After up... test if textBox.text is null
Hi, I want to test if the value in a text box is null. I tried if textBox.text = null and that is giving me syntax errors. Anyone that'll tell me how to do this please? I know it probably easy. -Mike if textbox.text = "" Microsoft ASP.NET Enthusiast Yeah, I'm cool with that --- which is what i used. But what if a user puts in a space, then you won't be looking for " " but instead for " "(2 spaces), right? Hi mparker1113, try out RequiredFieldValidator Happy programming Cheers life's Ng if Trim(textbox.text) = ""Micros...

Create Web Parts using
First of all i've never created any web part so i need a sample code that help me to know the process (step by step) to create web parts using Take a look at this post on my blog, the code example exists only in C#, but it will give you the basic understanding./Fredrik Normén - fredrikn @ twitterMicrosoft MVP, MCSD, MCAD, MCTASPInsidersMy Blog...

simple web application created in will not run
I tried to run the samples created by But it doesn't run successfully. For example, I copied VB project folder at C:\Inetpub\wwwroot\VB\Project. From the web browser I typed http://localhost/VB/Project/Project.aspx Only the text for some LabelControls showed up, not the content Text generated by project.aspx.vb. Does that mean that I didn't set the IIS and correct? Can you post the source code for your application. This may make it easier to debug what is going on. Thanks. Glen Kowalski Software Design Engineer / Test Microsoft Corporation This posti...

use VB.NET and C#.NET code in the same C#.NET project
All-- Here is a sample that is "off the beaten path", (at least for me). Is it possible, in an ASP.NET application, using the code-behind page building technique, to have both pages written in VB.NET and pages written C#.NET?At there is a sample that shows that, (contrary to popular belief), it IS possible to mix ASP.NET pages built with C#.NET with ASP.NET pages built with VB.NET in the same VS.NET 2003 project. Note that this is just a "fun" sample to see if it can be done and I do not recommend this practice as a "standard" way of develo...

Remove databinding from textbox in webform using
Environment: Visual Studio 2005 2.0 2005 I have a webform with a textbox that is bound to an sql server datbase field with the the following databinding:  bind("fieldname") Based on and event, which in this case is a change of an itemselected in dropdownlist outside the webform, I need to disable, clear, remove, or otherwise disengage the databinding on my textbox.  I've tried:  textbox.databindings.clear but apparently that doesn't work with webforms. Suggestions very welcome.  Officewonk     Vi...

how to use an inputbox to allow editing a textbox in
hi, i'm using a textbox in my webform.  i have placed two buttons edit and update(visible=false at pageload). when the user clicks on edit an input box should appear to enter a password so that the textbox becomes editable. the update button becomes visible.then the user can enter and update the textbox. on clicking the update button, the textbox again becomes read only.. how to use an input box and validate the password? or is there a better way to do so? only a password has to be entered to allow the textbox to become editable............plzzzz help me coz i have very less time to ...

Binding textboxes to dynamically created TableAdapter (VB.NET)
Hi All I started a form with the premise thath the following should be quite a straight forward thing to do, but it seems that there is no simple answer for this anywhere. I hope the experts in here can help me out. I have a form with various textboxes, showing sales lead details. Things like firstname, lastname, companyname etc. I then create the following in the Page_Load event Dim dsLeadFill As New DCAdminTableAdapters.CapturedLeadsTableAdapter dsLeadFill.GetDataByLeadID(Request.QueryString("LeadID")) The tableadapter should now be filled with the lead details I want. Now how do I u...

Creating C# web form in application
I have an application developed in In this application I want to create a webform where the code behind will be in C# as I have some functionality in C# I want to use. How can I achieve this? macca If you're using ASP.NET 2.0 this is simple- just add a new webform, and select language as c#. If you're using 1.1 or earlier, there isn't any way.My Blog...

Web resources about - Using the Textbox.Text on Dynmaicly Created Textboxes in VB.Net -

Resources last updated: 1/10/2016 9:01:58 PM