User Controls

T

tshad

I created a User Control that just does some VB code and doesn't have any
HTML in it.

Mainly it is an initialization piece that I want to put on all my pages. It
just has this code:

***************************************************
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack
if session("LoggedIn") <> nothing then
if session("firstName") <> nothing then
UserLoggedOn.Text = UserLoggedOn.Text & session("firstName")
if session("lastName") <> nothing then
UserLoggedOn.Text = UserLoggedOn.Text & " " & session("lastName")
end if
end if
UserLoggedOn.visible = true
UserLoggedOnLabel.visible = true
end if
if (Session("User") is nothing) orelse (Session("User").CompanyID is
DBNull.Value)
response.Redirect("/PageUnavailable.aspx")
end if
end if
End Sub
</script>
*********************************************************************

Do I still have to put the it in the HTML code section of my Web page?

Or can I just call it somehow from the Page_Load section of each page?

Thanks,

Tom
 
K

Ken Cox [Microsoft MVP]

Hi Tom,

Isn't this a case where you could create a class and call it when required
(perhaps just once if you can use master pages)?

For example, you could create a class like this and pass in the page context
and indicate the controls that need updating:

Imports Microsoft.VisualBasic
Imports System.Web

Public Class lgn
Sub chksession(ByVal cntxt As HttpContext, _
ByRef UserLoggedOn As TextBox, _
ByVal UserLoggedOnLabel As Label)
If cntxt.Session("LoggedIn") <> Nothing Then
If cntxt.Session("firstName") <> Nothing Then
UserLoggedOn.Text = UserLoggedOn.Text & cntxt.Session("firstName")
If cntxt.Session("lastName") <> Nothing Then
UserLoggedOn.Text = UserLoggedOn.Text & " " &
cntxt.Session("lastName")
End If
End If
UserLoggedOn.Visible = True
UserLoggedOnLabel.Visible = True
End If
If (cntxt.Session("User") Is Nothing) OrElse
(cntxt.Session("User").CompanyID Is DBNull.Value) Then
cntxt.Response.Redirect("/PageUnavailable.aspx")
End If
End Sub
End Class

and call it like this:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
Session("LoggedIn") = True
Session("firstName") = "Joe"
Session("lastname") = "Blow"
Dim login As New lgn
login.chksession(Context, UserLoggedOn, UserLoggedOnLabel)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Class</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="UserLoggedOn" runat="server"></asp:textbox>
<br />
<br />
<asp:label id="UserLoggedOnLabel" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>
 
T

tshad

Ken Cox said:
Hi Tom,

Isn't this a case where you could create a class and call it when required
(perhaps just once if you can use master pages)?

For example, you could create a class like this and pass in the page
context and indicate the controls that need updating:

Imports Microsoft.VisualBasic
Imports System.Web

Public Class lgn
Sub chksession(ByVal cntxt As HttpContext, _
ByRef UserLoggedOn As TextBox, _
ByVal UserLoggedOnLabel As Label)
If cntxt.Session("LoggedIn") <> Nothing Then
If cntxt.Session("firstName") <> Nothing Then
UserLoggedOn.Text = UserLoggedOn.Text & cntxt.Session("firstName")
If cntxt.Session("lastName") <> Nothing Then
UserLoggedOn.Text = UserLoggedOn.Text & " " &
cntxt.Session("lastName")
End If
End If
UserLoggedOn.Visible = True
UserLoggedOnLabel.Visible = True
End If
If (cntxt.Session("User") Is Nothing) OrElse
(cntxt.Session("User").CompanyID Is DBNull.Value) Then
cntxt.Response.Redirect("/PageUnavailable.aspx")
End If
End Sub
End Class

and call it like this:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
Session("LoggedIn") = True
Session("firstName") = "Joe"
Session("lastname") = "Blow"
Dim login As New lgn
login.chksession(Context, UserLoggedOn, UserLoggedOnLabel)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Class</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="UserLoggedOn" runat="server"></asp:textbox>
<br />
<br />
<asp:label id="UserLoggedOnLabel" runat="server"
text="Label"></asp:label></div>
</form>
</body>
</html>

That would work.

But I also want to make a control to do this and just add it in so that I
can use a different Page_Load routine and allow me to make changes only in
one place.

Thanks,

Tom
 

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

Top