Economize code

M

Morten Snedker

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
 
A

Alvin Bruney [MVP]

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.
 
M

Morten Snedker

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
 
M

Morten 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.

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
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top