This is a migrated thread and some comments may be shown as answers.

Multiple Text Boxes

4 Answers 123 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Birju
Top achievements
Rank 1
Birju asked on 01 Dec 2010, 10:26 PM
Dim insertQuantity As String
Dim insertEquipment As String
Dim intNumber As Integer = 1
Dim newControl As New Control
For intNumber = 1 To 1
    insertQuantity = "txtQuantity" + CStr(intNumber)
    insertEquipment = "txtEquipment" + CStr(intNumber)
    newControl = DirectCast(Me.FindControl(insertQuantity), TextBox)
    If (Not newControl Is Nothing) Then
        Response.Write("Cant find control ")
    Else
        Response.Write("Found the control")
        newControl = DirectCast(Me.FindControl(insertQuantity), TextBox)
    End If
Next intNumber
Hello There,

i am trying to retrieve values from multiple text boxes.  i Created the textboxes(quantity1, quantity2, etc)
and i am trying to pull these values when a user clicks the button.  This is what i have so far.

Please help.  When i access newControl.Text it is giving me a null reference to an instance of an object. Thank  you.

 

4 Answers, 1 is accepted

Sort by
0
Vinkel
Top achievements
Rank 1
answered on 02 Dec 2010, 09:24 AM
Do you have existing Control instance with id:

insertQuantity = "txtQuantity" + CStr(intNumber)

when you search for textbox? Debug your code and see.

-J
0
Birju
Top achievements
Rank 1
answered on 02 Dec 2010, 02:55 PM
Yes i do. I have the textboxes created in design view.

 

 

<td class="style2">
  <asp:Label ID="lblQuantity1" runat="server" Text="Quantity :"></asp:Label>
</td>
  
<td class="style3">
<asp:TextBox ID="txtQuantity1" runat="server"></asp:TextBox>
  
</td>

I tried different ways but i am getting the same error. Please help!!

 

0
Shinu
Top achievements
Rank 2
answered on 03 Dec 2010, 07:46 AM
Hi Birju,


The code should work if the controls are directly on page as it working for me. In your code I found that, you are showing "Cant find control" when control is Not Nothing (If (Not newControl Is Nothing) Then)  . Please have a look on this line and see whether you missed something.

Also, have you placed these controls inside any other container control, or is it directly on page?


-Shinu.
0
Birju
Top achievements
Rank 1
answered on 03 Dec 2010, 02:41 PM
Hey Shinu,

 Yes the controls are in a container.  Its on a <asp:content> tag.  I was trying to do something like Placeholder.Controls.Add(newTextBox).  But it wont pop up the properties when i write the statement. 

<

 

asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

 

 Thats what im referring to where the controls are on the page. Please advise.  Yes i switched the If statement around so it shows correctly.   How come i can not access the .text thats in txtQuantity1 and place it in newtextBox.Text? It gives the null reference to instance of an object.
Tags
General Discussions
Asked by
Birju
Top achievements
Rank 1
Answers by
Vinkel
Top achievements
Rank 1
Birju
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or