Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Simple asp.net programing question

Reply
Thread Tools

Simple asp.net programing question

 
 
Support
Guest
Posts: n/a
 
      01-27-2006
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


 
Reply With Quote
 
 
 
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      01-27-2006
(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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      01-27-2006
> 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Support
Guest
Posts: n/a
 
      01-27-2006
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:(E-Mail Removed)...
> (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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      01-27-2006
Ur right about the first point, my bad for not paying closer attention

http://www.google.com/search?hl=en&l...ge&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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:(E-Mail Removed)...
>> (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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      01-27-2006
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:(E-Mail Removed)...
> Ur right about the first point, my bad for not paying closer attention
>
> http://www.google.com/search?hl=en&l...ge&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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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:(E-Mail Removed)...
>>> (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" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me please : WepKey Changer Programing (Windows) Truty Wireless Networking 2 03-29-2006 10:52 PM
Server side programing question danarz@gmail.com NZ Computing 8 02-16-2005 10:57 AM
Some Programing Required Murray J Java 5 12-24-2004 06:00 PM
structural programing valli VHDL 2 11-15-2004 02:12 PM
First try at IOS programing mmm00k@yahoo.com Cisco 0 11-18-2003 09:43 PM



Advertisments