473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using Cookies in ASCX web controls

Hi all,

I had a weird issue with trying to use a cookie in a ASCX housed on my web
forms and I was wondering if I am missing something.

I wanted to use the authenticated cookie for the user name logged into the
web site to show the user that they are logged on. If I do it in the main
page (the ASPX that houses the ASCX) load event it works fine, but if I do
it in the Page Load event of the ASCX itself it gets weird results...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Request.Cookies("My_FullName") Is Nothing Then
If Request.Cookies("My_FullName").Value <> vbNullString Then
lblUser.Text = "<br>Logged in as:<br>" &
Request.Cookies("My_FullName").Value & "<br>"
Else
lblUser.Text = vbNullString
End If
Else
lblUser.Text = vbNullString
End If
End Sub

What *appears* to be happening is that the content of the ASCX is rendered
on the server so it's almost random what the value of the My_FullName cookie
is. Sometimes it's nothing, sometimes it's for a user who has never been
logged into this particular machine but is logged in at another machine,
sometimes it's correct.

Can someone please point me in the right direction to determine how ASCX's
get rendered? I'm sure it's just me not understanding the order in which
things work.

Ta
Andrew
Nov 18 '05 #1
1 2851
I would do the following:

1. Add a property in the ASCX for FullName
2. Add a private string in the ASCX file called _fullName

Private _fullName As String

Public Property FullName As String
Get
Return _fullName
End Get
Set(ByVal Value as String)
_fullName = Value
End Set
End Property

3. Use this property in the Page_Load in the ASCX

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not (_fullName Is Nothing) And (_fullName <> vbNullString) Then
lblUser.Text = "<br>Logged in as:<br>" & _fullName & "<br>"
Else
lblUser.Text = vbNullString
End If
End Sub

4. Add the control to the ASPX page. I assume MyControl1, something like:

<mc:MyControl id="MyControl1"></:MyControl>

5. Add a line for the control with the same name as the page control

Protected MyControl1 As MyControl

NOTE: <Exposure><Control_Name_In_Page> As <Control_Class>

6. With the declaration of the control in the CodeBehind, you can now set
properties. Something like:

If Not (Request.Cookies("My_FullName") Is Nothing) Then
MyControl1.FullName = Request.Cookies("My_FullName")
End If

This example is pretty rough, but it does expose one of the neater features
of .NET. Debug the app carefully, as there are times when the control will
not instantiate properly and you will want to move the layout to a composite
or server control.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Andrew Parsons" <ne********@parsonsdesigns.com> wrote in message
news:eA**************@TK2MSFTNGP10.phx.gbl...
Hi all,

I had a weird issue with trying to use a cookie in a ASCX housed on my web
forms and I was wondering if I am missing something.

I wanted to use the authenticated cookie for the user name logged into the
web site to show the user that they are logged on. If I do it in the main
page (the ASPX that houses the ASCX) load event it works fine, but if I do
it in the Page Load event of the ASCX itself it gets weird results...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Request.Cookies("My_FullName") Is Nothing Then
If Request.Cookies("My_FullName").Value <> vbNullString Then
lblUser.Text = "<br>Logged in as:<br>" &
Request.Cookies("My_FullName").Value & "<br>"
Else
lblUser.Text = vbNullString
End If
Else
lblUser.Text = vbNullString
End If
End Sub

What *appears* to be happening is that the content of the ASCX is rendered
on the server so it's almost random what the value of the My_FullName cookie is. Sometimes it's nothing, sometimes it's for a user who has never been
logged into this particular machine but is logged in at another machine,
sometimes it's correct.

Can someone please point me in the right direction to determine how ASCX's
get rendered? I'm sure it's just me not understanding the order in which
things work.

Ta
Andrew

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Jackson | last post by:
I'm using v2 of the UIP App. Block. I've created all my .aspx pages, all of which contain .ascx user controls. The .ascx user controls provide the standard server controls (i.e., LinkButtons, etc.)...
2
by: Ric | last post by:
im new to asp.net. from what i understand, you have the aspx file (presentation), user-control(ascx file), code-behind(vb file) and components(compiled vb and dll files). the aspx file contains a...
5
by: djscratchnsniffing | last post by:
i know you can access an ascx's properties/methods from an aspx file. Let's say you have an aspx file with two code-behind files(ascx files). Can you access one of the ascx file's...
2
by: Shawn Berg | last post by:
Some of my pages in the app I am developing inherit from a BasePage class I have created. I have done plenty of these in the past and they work fine. Now, however, I have an additional requirement....
6
by: David Bowey | last post by:
Hi There! I have som reusable ASCX controls that I also want to let my clients use in their websites. However, I don't want them to "have" the ASCX controls on their web servers. Instead I would...
2
by: Steve Richter | last post by:
I like the idea of being able to place server controls in an assembly and use them in all of my web applications. On the other hand I like the ease of using an .ascx file when I write user...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
0
by: mschep | last post by:
Hi, I built an assembly with a set of user controls. This can be done with the Visual Studio 2005 Deployment Project: building and merging for example all your aspx and ascx in one dll (lets...
5
by: Nathan Sokalski | last post by:
My Web.config file contains the following section to register some of my UserControls: <pages> <controls> <add tagPrefix="NATE" tagName="Banner" src="~/Banner.ascx"/> <add tagPrefix="NATE"...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.