Accessing values for Dynamically created controls

B

Brian Smith

I am attempting to create an ASP.Net web form to allow remote sites to
request an open relay for SMTP. I have a drop down list for the
number of IP addresses they want to request for. When they select the
number (1 – 500) I dynamically create textboxes to enter the IP
addresses. When the user has completed entering all of the
information and clicks the submit button, I hide the data entry panel
and display the review panel. This panel will allow them to review
their data and either select to modify the data or submit the data.
When the user selects to submit the data an E-Mail is sent to the
requestor and our team. I am not able to find a way access the .text
values of the dynamically created textboxes.



Any assistance would be greatly appreciated.
 
B

Branimir Giurov

Hi,
the problem you're facing comes most probably of the fact that you're losing
the view state of the dropdown before getting its value.
I'm aware of two ways to workaround this -

First - get the selected value in the Form_Load event handler from
Request.Form.
Second - you can use open source solution for dynamic controls available at
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

I think that there are also some articles in the MSDN which show how to
recreate the viewstate after form's submission, but the easier thing to do
will be one of those two.

Good luck :)

Branimir
 
B

Brian Smith

Thanks for the response.

Creating the dynamic controls is not the problem, I have already
completed that part.

First I declare the variables
Protected WithEvents srvip2 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip3 As System.Web.UI.WebControls.TextBox
and so on

Then I use the value supplied by the selection for the listbox list 1,
2, 3...

For intcounter = 2 To listBox.SelectedValue
PlaceHolder.Controls.Add(New LiteralControl("<p>Server " &
intcounter & ": "))
srvip2 = New TextBox
plhSrv.Controls.Add(txtTextbox)
Next

At this point the controls are created fine. I just can't do anything
with them.


The problem I am having is accessing the values held within the
dynamically created textbox. If I could increment srvipX when it is
created within the loop my problem would be solved.
 
B

Branimir Giurov

Hi again :)
The problem you're facing is much simpler than what I tried to explain :) -
sorry about that.
The easier way to do that is to perform following steps:
1. Place all the textboxes in some sort of a placeholder - PlaceHolder
control will work fine. Also can use a datagrid's cells ro whatever else you
feel comfortable with in the particular situation.
2.while itterating in the loop, use following line to get a reference to the
texbox with the index:

dim txt As TextBox
For intCounter=2 to ListBox.SelectedValue
txt = CType(placeHolderControlName.FindControl("srvip" & intCounter,
TextBox)
if ( txt is nothing ) 'findcontrol or casting failed
then
'
end if

Next

P.S. Sorry about potential errors in VB code - haven't done it in VB for
some time :)

Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC
 
B

Brian Smith

OK, it looks like my problem is with the post back. When the page posts
back, I lose everything. I have read about IPostBackDataHandler, but
I'm still very confused how to implement it. Can anyone assist? my
code in question is below.

Thanks,

Protected WithEvents srvip1 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip2 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip3 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip4 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip5 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip6 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip7 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip8 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip9 As System.Web.UI.WebControls.TextBox
Protected WithEvents srvip10 As System.Web.UI.WebControls.TextBox


Public Sub drpSrvNo_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
drpSrvNo.SelectedIndexChanged
For intcounter = 1 To drpSrvNo.SelectedValue
plhSrv.Controls.Add(New LiteralControl("<p>Server " &
intcounter & ": "))
txtTextbox = New TextBox
txtTextbox.ID = "SrvIP" & intcounter
plhSrv.Controls.Add(txtTextbox)
Next
End Sub
 

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,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top