Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get values from a user control that created by a loop

Status
Not open for further replies.

megkhp

Programmer
Mar 10, 2003
14
0
0
US
I have a user control (.ascx) that will show a list of locations based on the number from user input. For example, if enter 3, the page will list:
location 1
(textbox) enter address:
location 2
(textbox) enter address:
location 3
(textbox) enter address:

Since the user control is created by a loop, how do I get the values from all 3 textboxes from my web form? Any suggestion will be greatly appreciated.
 
Consider using a datagrid for this type of application. From what you've posted, some <TemplateColumn>s would be in order, and you'd be off and running.

It's much much simpler and ALOT cleaner than using UserControls.

Let us know if you need some direction on that.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
I'm guessing here..

When your user controls are created they are assigned an ID.

Store those ID into an array and store that in session

When you post back on that page get that array out of session and use page.FindControl() to then get the control

cast it back into a text box

and get the value

Good luck. Let us know how it goes
 
Thanks to both suggestions. Stsuing got that right, I figured that out after a several tries. Here's how I did it:

1. in .ascx html page, create a textbox.
<table>
<tr>
<td><asp:textbox ID="txtAddress" runat="server" /></td>
</tr>
</table>

2. in .ascx code behind, add a propery to the textbox.
Public Property Address() as String
Get
Return txtAddress.Text
End Get
Set(ByVal Value as String)
txtAddress.Text = Value
End Set
End Property

3. on the webform that call the .ascx, in the page_load:
Dim i As Integer
For i = 1 To 3
Dim c1 As UserControl = LoadControl("locations.ascx")
CType(c1, locations).ID = "ucLocation" + CStr(i)
phLocations.Controls.Add(c1)
Next

4. then reference to the following user control IDs to get the value of the 3 textboxes:
ucLocation1.Address
ucLocation2.Address
ucLocation3.Address
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top