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!

problem passing session variable to user control 1

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 1ocation 1, location 2, and location 3. My problem is that I cannot get the proper
number to list on the page, in this case the page will show location 3, location 3, and location 3. I put a for-loop in the page_load and in the .ascx file I use Session("locnum") to try to get the location number but it didn't work. Any suggestion will be greatly appreciated.

locations.ascx
--------------
<table>
<tr>
<td>Location # <%=Session("locnum") %></td>
</tr>
</table>

-------------------
Private Sub page_load()
Dim i As Integer
For i = 1 To 3
Session("locnum") = CStr(i)
Dim c1 As New UserControl
c1 = LoadControl("locations.ascx")
phLocations.Controls.Add(c1)
Next
 
Context.Session("locnum") doesn't work either.
 
Order of event firing is biting you here, and controlling that order is impossible. There are no guarantees on when Page_Load is going to fire when using LoadControl.

There are a few different ways you can tackle this problem, but I would suggest exposing a property on the user control, which you can then just assign to.
Code:
Private Sub page_load()
Dim i As Integer
For i = 1 To 3
   Dim c1 As New UserControl
   c1 = LoadControl("locations.ascx")
   c1.LocationNum = i
   phLocations.Controls.Add(c1)
Next

Also, I would run the assignment of that value in the PreRender event of the usercontrol, too, just to be sure.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top