Economize code

Discussion in 'ASP .Net' started by Morten Snedker, Jan 7, 2007.

  1. I have three web-controls (ascx) on my page, all containing graphics
    with a hyperlink. The information for these hyperlinks are stored in a
    database.

    On the Page_Load of each web-control I run the code below.

    What I can't figure out is if I can make it global, so that on each
    Page_Load I call the SetLinks procedure. Is this possible, given the
    content of SetLinks?

    Or should I consider a whole new approach?

    Sub SetLinks()

    Dim h As HyperLink
    Dim c As Control

    Dim da As New DataAccess
    Dim reader As SqlDataReader = da.AdList

    Try
    If reader.HasRows Then
    While reader.Read
    c = FindControl(reader(1).ToString)
    If Not c Is Nothing Then
    h = CType(c, HyperLink)
    h.NavigateUrl = reader(2).ToString
    h.Text = reader(3).ToString
    h.Target = reader(4).ToString
    End If
    End While
    End If
    Catch ex As Exception
    Response.Write(ex.Message)
    'don't throw exception to user
    'this should be handled through admin-part
    Finally
    reader.Close()
    da.Close()
    End Try

    End Sub


    Regards /Morten
    Morten Snedker, Jan 7, 2007
    #1
    1. Advertising

  2. Yup, sure. It's best if you modify the SetLink to accept the control as a
    parameter
    SetLink(Dim c As Control). That entire block of code can then be placed in a
    module or global file so that it is callable from every page.

    --
    Regards,
    Alvin Bruney
    ------------------------------------------------------
    Shameless author plug
    Excel Services for .NET is coming...
    OWC Black book on Amazon and
    www.lulu.com/owc

    "Morten Snedker" <> wrote in message
    news:...
    >I have three web-controls (ascx) on my page, all containing graphics
    > with a hyperlink. The information for these hyperlinks are stored in a
    > database.
    >
    > On the Page_Load of each web-control I run the code below.
    >
    > What I can't figure out is if I can make it global, so that on each
    > Page_Load I call the SetLinks procedure. Is this possible, given the
    > content of SetLinks?
    >
    > Or should I consider a whole new approach?
    >
    > Sub SetLinks()
    >
    > Dim h As HyperLink
    > Dim c As Control
    >
    > Dim da As New DataAccess
    > Dim reader As SqlDataReader = da.AdList
    >
    > Try
    > If reader.HasRows Then
    > While reader.Read
    > c = FindControl(reader(1).ToString)
    > If Not c Is Nothing Then
    > h = CType(c, HyperLink)
    > h.NavigateUrl = reader(2).ToString
    > h.Text = reader(3).ToString
    > h.Target = reader(4).ToString
    > End If
    > End While
    > End If
    > Catch ex As Exception
    > Response.Write(ex.Message)
    > 'don't throw exception to user
    > 'this should be handled through admin-part
    > Finally
    > reader.Close()
    > da.Close()
    > End Try
    >
    > End Sub
    >
    >
    > Regards /Morten
    Alvin Bruney [MVP], Jan 8, 2007
    #2
    1. Advertising

  3. On Sun, 7 Jan 2007 22:20:34 -0500, "Alvin Bruney [MVP]" <some guy
    without an email address> wrote:

    Don't know if there an English/US equalivant: sometimes you can't see
    the forrest for the trees. :)

    Thanks for your input.

    Regards /Snedker

    >Yup, sure. It's best if you modify the SetLink to accept the control as a
    >parameter
    >SetLink(Dim c As Control). That entire block of code can then be placed in a
    >module or global file so that it is callable from every page.
    Morten Snedker, Jan 8, 2007
    #3
  4. On Sun, 7 Jan 2007 22:20:34 -0500, "Alvin Bruney [MVP]" <some guy
    without an email address> wrote:

    >Yup, sure. It's best if you modify the SetLink to accept the control as a
    >parameter
    >SetLink(Dim c As Control). That entire block of code can then be placed in a
    >module or global file so that it is callable from every page.


    I can't figure out where to put the procedure so that it is runnable
    from all pages.

    E.g. the code

    While reader.Read
    c = FindControl(reader(1).ToString)
    If Not c Is Nothing Then
    h = CType(c, HyperLink)
    h.NavigateUrl = reader(2).ToString
    h.Text = reader(3).ToString
    h.Target = reader(4).ToString
    End If
    End While

    FindControl is not "usable" if put in a module of its own. I can't
    figure out how to make this code global for use on alle pages (ascx).

    Regards /Snedker
    Morten Snedker, Jan 8, 2007
    #4
  5. Put it in the global asax.cs. That is callable from anywhere.

    --
    Regards,
    Alvin Bruney
    ------------------------------------------------------
    Shameless author plug
    Excel Services for .NET is coming...
    OWC Black book on Amazon and
    www.lulu.com/owc

    "Morten Snedker" <morten_spammenot_ATdbconsult.dk> wrote in message
    news:...
    > On Sun, 7 Jan 2007 22:20:34 -0500, "Alvin Bruney [MVP]" <some guy
    > without an email address> wrote:
    >
    >>Yup, sure. It's best if you modify the SetLink to accept the control as a
    >>parameter
    >>SetLink(Dim c As Control). That entire block of code can then be placed in
    >>a
    >>module or global file so that it is callable from every page.

    >
    > I can't figure out where to put the procedure so that it is runnable
    > from all pages.
    >
    > E.g. the code
    >
    > While reader.Read
    > c = FindControl(reader(1).ToString)
    > If Not c Is Nothing Then
    > h = CType(c, HyperLink)
    > h.NavigateUrl = reader(2).ToString
    > h.Text = reader(3).ToString
    > h.Target = reader(4).ToString
    > End If
    > End While
    >
    > FindControl is not "usable" if put in a module of its own. I can't
    > figure out how to make this code global for use on alle pages (ascx).
    >
    > Regards /Snedker
    Alvin Bruney [MVP], Jan 8, 2007
    #5
    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. Ron
    Replies:
    1
    Views:
    2,696
    Showjumper
    Jun 24, 2003
  2. Ian
    Replies:
    0
    Views:
    1,379
  3. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    570
    Alphonse Giambrone
    Jun 28, 2003
  4. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,182
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  5. keithb
    Replies:
    1
    Views:
    888
    Bruce Barker
    Mar 29, 2006
Loading...

Share This Page