Problem using dynamically created textboxes

N

Nathan Sokalski

I am making a webform that allows the user to add another textbox by
clicking a button to allow them to enter however many names they need. The
procedure that does this uses information entered in the currently existing
textboxes. This works fine the first time they click the button, since the
first textbox is hardcoded. However, the second time they click the button I
recieve an error saying:

[NullReferenceException: Object reference not set to an instance of an
object.]
PAPresenters.register.btnAddName_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\PAPresenters\register.aspx.vb:128
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

When I do a view source on the page, it gave the generated textboxes the ID
that I told it to. I think the problem has something to do with ASP.NET
forgetting the new controls when it looks for them the second time or
something. The line of code where I recieve the error is:

Line 126: 'Make sure there are no empty name fields
Line 127: For i As Integer = 0 To namecount
Line 128: If CType(FindControl("txtFName" & CStr(i)),
TextBox).Text = "" OrElse CType(FindControl("txtLName" & CStr(i)),
TextBox).Text = "" Then
Line 129: Exit Sub
Line 130: End If

Why is ASP.NET unable to find the dynamically created controls? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top