Using Cookies in ASCX web controls

A

Andrew Parsons

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
 
C

Cowboy \(Gregory A. Beamer\)

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!
**********************************************************************
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top