Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Economize code

Reply
Thread Tools

Economize code

 
 
Morten Snedker
Guest
Posts: n/a
 
      01-07-2007
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
 
Reply With Quote
 
 
 
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      01-08-2007
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



 
Reply With Quote
 
 
 
 
Morten Snedker
Guest
Posts: n/a
 
      01-08-2007
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.


 
Reply With Quote
 
Morten Snedker
Guest
Posts: n/a
 
      01-08-2007
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
 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      01-08-2007
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



 
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
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM
Re: Code Behind vs. no code behind: error Ben Miller [msft] ASP .Net 1 06-28-2003 01:46 AM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ian ASP .Net 0 06-25-2003 01:14 PM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ron ASP .Net 1 06-24-2003 07:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57