<form id="form1" runat="server">
<br />
<asp:TextBox ID="txt" runat="server">1</asp:TextBox>
<asp:TextBox ID="txt2" runat="server">2</asp:TextBox>
<asp:TextBox ID="txt3" runat="server">3</asp:TextBox>
</form>
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xxx As Integer
Dim txt As TextBox
For xxx = 1 To 10
txt = DirectCast(Me.FindControl("textbox" & xxx), TextBox)
Response.Write(txt.Text)
Next xxx
End Sub
Error ที่ได้
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 18: For xxx = 1 To 10
Line 19: txt = DirectCast(Me.FindControl("textbox" & xxx), TextBox)
Line 20: Response.Write(txt.Text)
Line 21:
Line 22: Next xxx
Dim xxx As Integer
Dim txt As TextBox
For xxx = 1 To 10
txt = DirectCast(Me.FindControl("textbox" & xxx),TextBox)
IF Not IsNothing(txt) Then
Response.Write(txt.text)
End IF
Next xxx