I am trying to loop through my repeater control and get the textbox values.
However, I am getting an error:{"Object reference not set to an instance of an object."}
my code is:
Dim txtField As TextBox
Dim j As Integer = 0
'Confirm if user has entered atleast one quantity
For Eac开发者_如何学Ch item In rptRequestForm.Items
txtField = rptRequestForm.FindControl("txtBox")
If txtField.Text <> Nothing Then
j += 1
Else
End If
Next
UPDATE: aspx code is:
<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>
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're trying to access the ".Text" property of a TextBox that it didn't find. The TextBox itself is the object to which there is no reference.
Incidentally, the .Text property of an actual Textbox (one that exists and was found) can't be "Nothing". It can only be String.Empty or a valid string.
Edited my line of code
Sorry, my VB is rusty.
Final edit
AARGH! I'm blind. I can't believe I didn't see this. There were TWO problems withthe original code. This is the answer to the second issue:
Change
txtField = rptRequestForm.FindControl("txtBox")
to
txtField = item.FindControl("txtBox")
The ITEM has to find the control, not the repeater itself!
I created a small web app just to check to see if I was grabbing the textbox's text and finally found the issue above. my code is NOT the same as yours in the aspx, but here's a complete code listing so that 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>
produces the following output in the System.Diagnostics.Debug window:
Item
Frank
AlternatingItem
Dave
Item
Muhammad
The thread 0x321c has exited with code 0 (0x0).
The thread 0x39b8 has exited with code 0 (0x0).
You have to properly cast it as Textbox
e.g.
TextBox txtField = (TextBox)rptRequestForm.FindControl("txtBox") // C# code
Here is VB.NET code:
Dim txtField As TextBox = CType(rptRequestForm.FindControl("txtBox"), TextBox)
Dim myText as string
Dim j As Integer = 0
'Confirm if user has entered atleast 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 wouldn't use nothing -- not sure that causes a problem or not, but usually I see that for objects, not properties. String.IsNullOrNothing() is made for checking strings for null or empty ("").
You don't need to worry about whether or not the textbox exists, because if it exists in one row of the repeater, it will exist in all rows. I guess you could check it for 'nothing' if you weren't sure what "txtBox" was at design time...but otherwise, not necessary.
You should definately use the cast (CTYPE()). I think you might be able to get away with not using it if all you want is .text, but the CTYPE gives you access to all of the textbox's properties (not just it's inherited properties), and also, you might need to do checkboxes or other controls at some point where you pretty much have to CTYPE in order to get to .ischecked, etc.
I made a generic method for set the 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
精彩评论