Wednesday, March 28, 2012

Simple asp.net programing question

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(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
--
http://www.openmymind.net/

"Support" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
news:uwlKVxzIGHA.1288@.TK2MSFTNGP09.phx.gbl...
> 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
> 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.

"Support" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
news:uwlKVxzIGHA.1288@.TK2MSFTNGP09.phx.gbl...
> 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
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:u43q45zIGHA.1028@.TK2MSFTNGP11.phx.gbl...
> (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
> --
> http://www.openmymind.net/
>
> "Support" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
> news:uwlKVxzIGHA.1288@.TK2MSFTNGP09.phx.gbl...
>> 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
>>
>>
Ur right about the first point, my bad for not paying closer attention :)

http://www.google.com/search?hl=en&...age&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" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
news:u0v9RG0IGHA.3904@.TK2MSFTNGP10.phx.gbl...
> 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:u43q45zIGHA.1028@.TK2MSFTNGP11.phx.gbl...
>> (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
>> --
>> http://www.openmymind.net/
>>
>>
>>
>> "Support" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
>> news:uwlKVxzIGHA.1288@.TK2MSFTNGP09.phx.gbl...
>>> 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
>>>
>>>
>>
>>
Oopss..copied the wrong link:
http://aspnet.4guysfromrolla.com/articles/041305-1.aspx

Karl

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

"Karl Seguin [MVP]" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:e2QPUp0IGHA.984@.tk2msftngp13.phx.gbl...
> Ur right about the first point, my bad for not paying closer attention :)
> http://www.google.com/search?hl=en&...age&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" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
> news:u0v9RG0IGHA.3904@.TK2MSFTNGP10.phx.gbl...
>> 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:u43q45zIGHA.1028@.TK2MSFTNGP11.phx.gbl...
>>> (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
>>> --
>>> http://www.openmymind.net/
>>>
>>>
>>>
>>> "Support" <RemoveThis_Support@.mail.oci.state.ga.us> wrote in message
>>> news:uwlKVxzIGHA.1288@.TK2MSFTNGP09.phx.gbl...
>>>> 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
>>>>
>>>>
>>>
>>>
>>
>>

0 comments:

Post a Comment