Simple asp.net programing question

S

Support

Hello:

I have the following code that currently needs to go in every page.

QUESTION 1) Can Page_Preinit go somewhere else more global like Global.asax
so that I dont have to copy this code in every page

QUESTION 2) How can I externalize this code, ie, can I create a class ?...
some external code that would be located in one location only for ease of
maintenance that each page could call ? IF so, please describe in detail.

Thanks

Public Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
Dim page As Page
page = DirectCast(sender, Page)
If Session("ScreenResolution") = "" Then
Session("path") = Request.Path
Response.Redirect("/Internals/Templates/detectscreen.aspx")
End If


If Session("ScreenResolution") = "800" Then
page.MasterPageFile = "/Internals/Templates/800MasterPage.master"
Else
page.MasterPageFile = "/Internals/Templates/1024MasterPage.master"
End If


End Sub
 
K

Karl Seguin [MVP]

(a) You can use a shared base class for all your pages or a master page.

(b) You could just create a function, but I think the first solution is
better

public class utility
public sub shared SetMasterPage()
dim context as HttpContext = HttpContext.Current;
dim page as Page = ctype(context .Handler, Page)
'write the rest of your code, Response/Session/Request can all be
accessed via context.Response, context.Session, context.Request
end sub
end class

call it via Utility.SetMasterPage() but again, I think if you can work out
how to do is ala (A), you'll be better off.

Karl
 
K

Kevin Spencer

QUESTION 1) Can Page_Preinit go somewhere else more global like
Global.asax so that I dont have to copy this code in every page

You can create a base class that inherits System.Web.UI.Page, which has this
method running in it, and inherit that class for all of your Pages.
QUESTION 2) How can I externalize this code, ie, can I create a class ?...
some external code that would be located in one location only for ease of
maintenance that each page could call ? IF so, please describe in detail.

This is the same as the answer to your first question. How you create the
class depends upon the coding model you're using, as well as the tools you
have.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 
S

Support

Thanks Karl:
As i understand things - since I am choosing which master page to use based
upon screen resolution - the code could not be tied to the master page (I
think...) so this seems to lead me to having a shared base class for all my
pages ....
Any articles you know of on how to do that ?
Thanks
Terry

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 
K

Karl Seguin [MVP]

Ur right about the first point, my bad for not paying closer attention :)

http://www.google.com/search?hl=en&...GGL:en&q=asp.net+common+base+page&btnG=Search

relates to 1.1, but should be applicable in 2.0 all the same.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Support said:
Thanks Karl:
As i understand things - since I am choosing which master page to use
based upon screen resolution - the code could not be tied to the master
page (I think...) so this seems to lead me to having a shared base class
for all my pages ....
Any articles you know of on how to do that ?
Thanks
Terry

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top