Simple asp.net programing question

Discussion in 'ASP .Net' started by Support, Jan 27, 2006.

  1. Support

    Support Guest

    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
    Support, Jan 27, 2006
    #1
    1. Advertising

  2. (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" <> wrote in message
    news:...
    > 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
    >
    >
    Karl Seguin [MVP], Jan 27, 2006
    #2
    1. Advertising

  3. > 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" <> wrote in message
    news:...
    > 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
    >
    >
    Kevin Spencer, Jan 27, 2006
    #3
  4. Support

    Support Guest

    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:...
    > (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" <> wrote in message
    > news:...
    >> 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
    >>
    >>

    >
    >
    Support, Jan 27, 2006
    #4
  5. 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" <> wrote in message
    news:...
    > 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:...
    >> (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" <> wrote in message
    >> news:...
    >>> 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
    >>>
    >>>

    >>
    >>

    >
    >
    Karl Seguin [MVP], Jan 27, 2006
    #5
  6. 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:...
    > 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" <> wrote in message
    > news:...
    >> 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:...
    >>> (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" <> wrote in message
    >>> news:...
    >>>> 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
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Karl Seguin [MVP], Jan 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valli

    structural programing

    valli, Nov 15, 2004, in forum: VHDL
    Replies:
    2
    Views:
    2,174
    Jim Lewis
    Nov 15, 2004
  2. Murray J

    Some Programing Required

    Murray J, Dec 18, 2004, in forum: Java
    Replies:
    5
    Views:
    311
  3. chandu
    Replies:
    3
    Views:
    393
    Matt Humphrey
    Mar 2, 2006
  4. zegga
    Replies:
    0
    Views:
    525
    zegga
    Aug 1, 2009
  5. noor

    Low level programing in ASP .Net

    noor, Dec 11, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    122
Loading...

Share This Page