473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Setting link dynamically

My code works as intended. I'm just curious if I am approaching it the
right way.

Images have a link upon them. These links can be changed, stored and
retrieved by a stored procedure. I use FindControl to set the
properties of each image. Is that the proper way to do it?
Regards /Snedker

Dim sqlCon As New SqlConnection, reader As SqlDataReader
sqlCon.ConnectionString = conString

Dim h As HyperLink

Try
Dim cmd As New SqlCommand
cmd.CommandText = "spSetAdLinks"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = sqlCon

sqlCon.Open()

reader = cmd.ExecuteReader

If Not reader.HasRows Then
Response.Write("No records")
Else
While reader.Read
h = FindControl(reader(1).ToString)
h.NavigateUrl = reader(2).ToString
h.Text = reader(3).ToString
h.Target = reader(4).ToString
End While
End If
Jan 2 '07 #1
7 1008
Ya..it's a fine way to do it.

Your code is a little rough though. The worst is that you don't have Option
Strict On, it's convinient in some cases, but will quickly become way more
trouble. Turn it on and fix the errors it reports.

Your data access layer and presentation layers are glued..which is fine for
a small project.

Your objects aren't being disposed of (although I see a try, I figure you
just left out the finally for clarities sake..).

Cheers,
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <fi**@alotofsites.comwrote in message
news:k7********************************@4ax.com...
My code works as intended. I'm just curious if I am approaching it the
right way.

Images have a link upon them. These links can be changed, stored and
retrieved by a stored procedure. I use FindControl to set the
properties of each image. Is that the proper way to do it?
Regards /Snedker

Dim sqlCon As New SqlConnection, reader As SqlDataReader
sqlCon.ConnectionString = conString

Dim h As HyperLink

Try
Dim cmd As New SqlCommand
cmd.CommandText = "spSetAdLinks"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = sqlCon

sqlCon.Open()

reader = cmd.ExecuteReader

If Not reader.HasRows Then
Response.Write("No records")
Else
While reader.Read
h = FindControl(reader(1).ToString)
h.NavigateUrl = reader(2).ToString
h.Text = reader(3).ToString
h.Target = reader(4).ToString
End While
End If
Jan 2 '07 #2

If all o fthe images are in the same container like a panel, you could
do panel.FindControl instead of Me.FindControl and then it'll be
searching a smaller collection.

Also you should handle what happens if FindControl returns Nothing.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Tue, 02 Jan 2007 20:11:08 +0100, Morten Snedker
<fi**@alotofsites.comwrote:
>My code works as intended. I'm just curious if I am approaching it the
right way.

Images have a link upon them. These links can be changed, stored and
retrieved by a stored procedure. I use FindControl to set the
properties of each image. Is that the proper way to do it?
Regards /Snedker

Dim sqlCon As New SqlConnection, reader As SqlDataReader
sqlCon.ConnectionString = conString

Dim h As HyperLink

Try
Dim cmd As New SqlCommand
cmd.CommandText = "spSetAdLinks"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection = sqlCon

sqlCon.Open()

reader = cmd.ExecuteReader

If Not reader.HasRows Then
Response.Write("No records")
Else
While reader.Read
h = FindControl(reader(1).ToString)
h.NavigateUrl = reader(2).ToString
h.Text = reader(3).ToString
h.Target = reader(4).ToString
End While
End If
Jan 2 '07 #3
Auch, you're evil! :-)

I suppose you by rough refer to something like

h = FindControl(reader(1).ToString)

, which I guess you knew would produce an error due to the implicit
type conversion, when having Option Strict set. Though, I just can't
figure out how to do the casting that makes i work..? And I really
tried! :-)
Regards /Snedker
On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:
>Ya..it's a fine way to do it.

Your code is a little rough though. The worst is that you don't have Option
Strict On, it's convinient in some cases, but will quickly become way more
trouble. Turn it on and fix the errors it reports.

Your data access layer and presentation layers are glued..which is fine for
a small project.

Your objects aren't being disposed of (although I see a try, I figure you
just left out the finally for clarities sake..).

Cheers,
Karl
Jan 2 '07 #4
On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:

As where a bigger project would be better approached how?

I'm fairly new to aspnet (and websites in general), so please be as
specific as possible as I'm sucking up all knowledge I stumble upon.
>Your data access layer and presentation layers are glued..which is fine for
a small project.
Jan 2 '07 #5
Sam's comments were very good also.

As for casting..

dim control as Control = FindControl(reader(1).ToString())
if (control is nothing) then
'throw an exception maybe? depends whether this is exceptional or not
else if (control is HtmlImage)
dim h as HtmlImage = ctype(control, HtmlImage)
h.NavigateUrl = ...
else if (control is TextBox)
dim t as TextBox = ctype(control, TextBox)
t.Text = reader(1).ToString()
end if

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <fi**@alotofsites.comwrote in message
news:ve********************************@4ax.com...
Auch, you're evil! :-)

I suppose you by rough refer to something like

h = FindControl(reader(1).ToString)

, which I guess you knew would produce an error due to the implicit
type conversion, when having Option Strict set. Though, I just can't
figure out how to do the casting that makes i work..? And I really
tried! :-)
Regards /Snedker
On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:
>>Ya..it's a fine way to do it.

Your code is a little rough though. The worst is that you don't have
Option
Strict On, it's convinient in some cases, but will quickly become way more
trouble. Turn it on and fix the errors it reports.

Your data access layer and presentation layers are glued..which is fine
for
a small project.

Your objects aren't being disposed of (although I see a try, I figure you
just left out the finally for clarities sake..).

Cheers,
Karl
Jan 3 '07 #6
At the very least, it's generally a good idea to have your dataaccess layer
sit in it's own class which your codebehind files can leverage.

something like:

Button_Click()
dim dal as new DataAccess()
dal.AddUser(User.Text, Password.Text)
end sub

or something..

For larger projects, you typically want a full domain layer...so you do
something like:

dim user as User = User.CreateNewUser();
user.FirstName = FirstName.Text;
user.LastName = LastName.Text;
user.Save();

I left out the validation you'd normally have..

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Morten Snedker" <fi**@alotofsites.comwrote in message
news:nf********************************@4ax.com...
On Tue, 2 Jan 2007 14:49:00 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:

As where a bigger project would be better approached how?

I'm fairly new to aspnet (and websites in general), so please be as
specific as possible as I'm sucking up all knowledge I stumble upon.
>>Your data access layer and presentation layers are glued..which is fine
for
a small project.
Jan 3 '07 #7
On Wed, 3 Jan 2007 08:25:39 -0500, "Karl Seguin"
<ka********@removeopenmymindremovemetoo.andmenetwr ote:

It was the CType thing I'd forgotten all about. Funny how it slips
when you've been away from it for only a short while.

Thanks to both you and Sam for your fine resonse.
Regards /Morten
Jan 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: C A Upsdell | last post by:
I have a site where I am setting a style dynamically, using the JS statement: obj.style.backgroundImage = 'url(img/bak_page.jpg)'; where 'obj' is either document.getElementById(id), or...
3
by: CSDunn | last post by:
Hello, I currently have an Access 2003 ADP Report/Subreport set up in which I have 12 subreports in a single main report that are located in a group header called 'PermnumHeader' (Permnum would be...
2
by: brw | last post by:
Is there a way to dynamically add a link tag to the head block of an ..aspx page? I'm aware that you can add a link tag (or literal control) statically and then dynamically modify the attributes....
8
by: simon | last post by:
On code behind file: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim ctrl As New LinkButton ctrl.ID =...
9
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all...
6
by: jim | last post by:
Is anyone able to provide me with a link to useful documentation or just outright explain to me how to set query parameters dynamically? I'm really new to Access and databases in general but I...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
8
by: Jeff | last post by:
ASP.NET 2.0 I'm wondering how to set the color of a visited HyperLinkField (the link text) in a GridView?? Here is the markup of the HyperLinkField I have problems with: <asp:HyperLinkField...
3
by: pbd22 | last post by:
Hi. How do I add the runat=server attribute on a buttonfield link dynamically? thanks!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.