Run through a repeater control to get text field values ​​in asp.net

I am trying to skip my repeater and get the values ​​of a text field.
However, I get an error message:

{"The reference to the object is not installed in the instance of the object." }

my code is:

Dim txtField As TextBox Dim j As Integer = 0 'Confirm if user has entered atleast one quantity For Each item In rptRequestForm.Items txtField = rptRequestForm.FindControl("txtBox") If txtField.Text <> Nothing Then j += 1 Else End If Next 

UPDATE: aspx code:

  <td><asp:Repeater ID="rptRequestForm" runat="server"> <HeaderTemplate> <table border="0" width="100%"> <tr> <td style="width:50%" class="TextFontBold"><asp:Label runat="server" ID="Label1" Text="Product"></asp:Label></td> <td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label2" Text="Quantity"></asp:Label></td> <td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label3" Text="Price (ea.)"></asp:Label></td> </tr> </table> </HeaderTemplate> <ItemTemplate> <table border="0" width="100%"> <tr> <td style="width:50%" class="TextFont"><span><%#Trim(Eval("Product_Title"))%></span></td> <td style="width:25%"><asp:TextBox ID="txtBox" runat="server" Width="30%" onblur="Javascript:numberonly(this)"></asp:TextBox></td> <td style="width:25%" class="TextFont"><span><%#Trim(FormatCurrency(Eval("Price")))%></span></td> </tr> </table> </ItemTemplate> </asp:Repeater> 
+7
source share
4 answers

to try

 Dim someString as String = "Not set" <-- used later to hold the values of the string Dim txtField As TextBox Dim j As Integer = 0 'Confirm if user has entered atleast one quantity For Each item In rptRequestForm.Items txtField = item.FindControl("txtBox") If Not IsNothing(txtField) Then ' <--- this is the line I changed j += 1 someString = txtField.Text ' <-- once you've checked and know that the textbox exists, you just grab the value like so. ' do whatever you like with the contents of someString now. Else End If Next 

The problem is that you are trying to access the ".Text" property of a text field that it did not find. The TextBox itself is an object for which there is no link.

By the way, the .Text property of a real text field (which exists and was found) cannot be "Nothing". It can be only String.Empty or a valid string.

Edited my line of code

Sorry, my VB is rusty.

Final editing

Aargh! I am blind. I can’t believe that I didn’t see this. There were two problems with the source code. This is the answer to the second question:

Edit

 txtField = rptRequestForm.FindControl("txtBox") 

to

 txtField = item.FindControl("txtBox") 

ITEM must find the control, not the repeater itself!

I created a small web application to check if I was grabbing the text of a text field and finally found the problem above. my code is NOT the same as yours in aspx, but here is a complete list of codes so you can see how it works:

vb code

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim t As New System.Data.DataTable t.Columns.Add("Name") Dim newRow(1) As Object t.Rows.Add(New Object() {"Frank"}) t.Rows.Add(New Object() {"Dave"}) t.Rows.Add(New Object() {"Muhammad"}) rptRequestForm.DataSource = t rptRequestForm.DataBind() Dim txtField As TextBox Dim j As Integer = 0 'Confirm if user has entered atleast one quantity For Each item As RepeaterItem In rptRequestForm.Items txtField = item.FindControl("txtBox") If Not IsNothing(txtField) Then ' <--- this is the line I changed j += 1 System.Diagnostics.Debug.WriteLine(item.ItemType.ToString()) System.Diagnostics.Debug.WriteLine(txtField.Text) Else System.Diagnostics.Debug.WriteLine(item.ItemType.ToString()) End If Next End Sub 

aspx code

 <asp:Repeater ID="rptRequestForm" runat="server"> <HeaderTemplate> Hello! </HeaderTemplate> <ItemTemplate> <asp:TextBox ID="txtBox" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox> <br /> </ItemTemplate> </asp:Repeater> 

displays the following result in the System.Diagnostics.Debug window:

Paragraph

Franc

AlternatingItem

Dave

Paragraph

Muhammad

Thread 0x321c exited with code 0 (0x0).

Thread 0x39b8 exited with code 0 (0x0).

+12
source

You must use it correctly as a Textbox , for example.

 TextBox txtField = (TextBox)rptRequestForm.FindControl("txtBox") // C# code 

Here is the VB.NET code:

 Dim txtField As TextBox = CType(rptRequestForm.FindControl("txtBox"), TextBox) 
+2
source
 Dim myText as string Dim j As Integer = 0 

'Confirm user has entered at least one quantity

 For Each myItem as repeateritem In rptRequestForm.Items If NOT string.isnullorempty(CTYPE(myItem.FindControl("txtBox"),textbox).text) then j += 1 End If Next 

I would not use anything - I'm not sure what causes the problem or not, but usually I see this for objects, not for properties. String.IsNullOrNothing () is executed to check strings for null or empty ("").

You do not need to worry about whether or not a text field exists, because if it exists on one line of the repeater, it will exist on all lines. I think you could check it for “nothing” if you weren’t sure that the “txtBox” was during development ... but otherwise it is not necessary.

You should definitely use listing (CTYPE ()). I think you can leave without using it if all you need is a .text, but CTYPE gives you access to all the properties of the text field (and not just the inherited properties), and you might also need checkboxes or other controls at some point when you pretty much need CTYPE to get to .ischecked, etc.

+1
source

I made the general method for defining a property visible, I think you can take it as an example

 Sub SetVisibleControlRepeater(ByRef repetidor As Repeater, ByVal idControl As String, ByVal esVisible As Boolean) For Each item As RepeaterItem In repetidor.Items Dim boton As System.Web.UI.WebControls.Button = CType(item.FindControl(idControl), Button) boton.Visible = esVisible Next End Sub 
0
source

All Articles