?
Solved

Hyperlink in gridview not working correctly

Posted on 2014-07-11
27
Medium Priority
?
805 Views
Last Modified: 2014-07-14
I have a gridview in my ASP.NET website(VB code) and my last column holds the website of the companies that I am loading into the grid. When I click on the hyperlink Im loading a new tab with the webpage link I just clicked on. The thing is, if the hyperlink has an HTTP the page loads fine but if the hyperlink is a www address it takes the path of the project for some reason and tries to load that with the www and of course that doesnt work.
For instance, the hyperlink I am trying to click on is "www.abc123.com/fa" and when I click on it, or hover over it, I see on the bottom of the page that it has "http://localhost:58626/Trans/www.abc123.com/fa" and everything before the www is the path of my website? But like I said if the hyperlink starts with an http it comes up fine and even shows on the bottom of the page, when I hover over it, the correct site?
My first thought was to check the Gridview RowCommand or RowDataBound event when I click on the hyperlink and fix the path of the site if it starts with www take everything off the front of it and just load the site?
Has this happened to anyone?

Please help me!!

Thanks
JK
0
Comment
Question by:jknj72
  • 15
  • 12
27 Comments
 

Author Comment

by:jknj72
ID: 40190438
Just an fyi, this is the markup code for my hyperlink in my gridview. Maybe im doing something wrong here???

<asp:TemplateField HeaderText="Web Site" ItemStyle-HorizontalAlign="Left" HeaderStyle-Width="125">            
     <ItemTemplate>                
     <asp:HyperLink ID="hlWebsite" runat="server" Target = "_blank" Text='<%# Eval("SUPPORTWEBSITE") %>'        NavigateUrl='<%# Eval("SUPPORTWEBSITE") %>'>
    </asp:HyperLink>            
   </ItemTemplate>  
   <HeaderStyle HorizontalAlign="Left" />      
</asp:TemplateField>
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190475
something like this?

<asp:HyperLinkField ID="hlWebsite" DataNavigateUrlFields="SUPPORTWEBSITE" DataNavigateUrlFormatString="gotopage.aspx?p1={0}" Text="Web Site">

Open in new window

0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190477
Then add your Target = "_blank" there.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:jknj72
ID: 40190518
"Then add your Target = "_blank" there. "

Where should I be adding this?
0
 

Author Comment

by:jknj72
ID: 40190521
Also this doesnt seem to be working??? It doesnt recognize HyperLinkField so I assumed you meant Hyperlink and then the DataNavigateUrlFields and DataNavigateUrlFormatString are not valid attributes...Can you please explain?

<asp:HyperLinkField ID="hlWebsite" DataNavigateUrlFields="SUPPORTWEBSITE" DataNavigateUrlFormatString="gotopage.aspx?p1={0}" Text="Web
0
 

Author Comment

by:jknj72
ID: 40190530
My last comment  wasnt working because it didnt have an end tag but I got that to work now. So disregard the last comment but I am still at

"Then add your Target = "_blank" there. "
 Where should I be adding this?

Can you help me with this??
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190576
Anywhere within that line should suffice.

<asp:HyperLinkField ID="hlWebsite" DataNavigateUrlFields="SUPPORTWEBSITE" DataNavigateUrlFormatString="gotopage.aspx?p1={0}" Target = "_blank" Text="Web Site">

Open in new window

0
 

Author Comment

by:jknj72
ID: 40190610
But I need to put the value into the gridview column with the website

 Text='<%# Eval("SUPPORTWEBSITE") %>'

But when I add this it says the HyperLinkField does not have a DataBinding event

After I get this figured out I simply wanna click on the link and bring up the site that is in the text?

I tried changing the text but now Im getting errors??? I think Im close but I need some help still...
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190620
Ok try this:

                <asp:TemplateField HeaderText="Web Site" SortExpression="SUPPORTWEBSITE">
                 <ItemTemplate>
                   <asp:HyperLink ID="hlWebsite" runat="server" Target="_blank" HeaderText="Web Site" NavigateUrl='<%# Eval("ID","gotopage.aspx?ID={0}") %>' Text = '<%# Eval("SUPPORTWEBSITE") %>' />
                 </ItemTemplate> 
                </asp:TemplateField>

Open in new window

0
 

Author Comment

by:jknj72
ID: 40190682
It bombed on
NavigateUrl='<%# Eval("ID","gotopage.aspx?ID={0}") %>'
It doesnt recognize "ID"??

And when I hover over the links they all have the path to the website before the actual URL

So when I hover over www.abc123.com it tries to open
http://localhost:58626/Trans/gotopage.aspx?ID=www.abc123.com/fa
And I need to open
www.abc123.com/fa

I think we are almost there...What do you think???
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190692
First, you need a unique identifier for *each* record (hyperlink).

Do you have some sort of primary key on your db?

Replace ID with whatever your primary key is.

Also, replace gotopage.aspx with the page your users will be taken to *after* clicking the link.

Do these things and your stuff will work.

That is *WORKING* example I posted.
0
 

Author Comment

by:jknj72
ID: 40190810
"Also, replace gotopage.aspx with the page your users will be taken to *after* clicking the link."

The page that the user will be taken to is the page of the link they will click on? So should I replace the gotopage.aspx with the column name?

Ex:
<asp:HyperLink ID="hlWebsite" runat="server" Target="_blank" HeaderText="Web Site" NavigateUrl='<%# Eval("VENDORID","SUPPORTWEBSITE") %>' Text = '<%# Eval("SUPPORTWEBSITE") %>' />
0
 

Author Comment

by:jknj72
ID: 40190830
Im doing it wrong. Its trying to pull up the column name and not the site name that the column value.
I put  
<asp:HyperLink ID="hlWebsite" runat="server" Target="_blank" HeaderText="Web Site" NavigateUrl='<%# Eval("VENDORID","SUPPORTWEBSITE") %>' Text = '<%# Eval("SUPPORTWEBSITE") %>' />

When I hover now its pointing to
http://localhost:58626/Trans/SUPPORTWEBSITE(this is a column name and not the column value)
So something is till not right
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190862
Kind of look at it like this:

Your user is on a landing page called landing.aspx

On this landing page, you want the user to click on a link to go to another page to view something info.

So on landing page, you

<a href="processpage.aspx?id=1234>go to process page</a>

Open in new window


User clicks on go to process page, and will be taken to processpage.aspx.

Infact, if you look at the browser, you will see something like:

Processpage.aspx?1234=something.

Maybe, you can explain what you are trying to achieve when a user clicks on the link.

This is not right:
NavigateUrl='<%# Eval("VENDORID","SUPPORTWEBSITE") %>'

Open in new window

0
 

Author Comment

by:jknj72
ID: 40190911
I hear what you are saying but I am not going to a page within my website I am going to the site in the link

Ok......I have a grid and In the grid I am populating with Vendor information as follows.....

Vendor      Address               City          State       Zip            Web Site

ABC          123 Main Street     Newark     NJ           07652       www.abc123.com

In the column "Web Site" I have a hyperlink with the Vendors website in the column value...(www.abc123.com)

When they click on the value in the Web Site column...I want to go to "www.abc123.com" in a new tab....That is all I want to happen
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40190960
So, on your database, SUPPORTWEBSITE is a columnName and it has a value of www.abc123.com?

If so, then this should work, at least the link should have the value, not the columnane:

                <asp:TemplateField HeaderText="Web Site" SortExpression="SUPPORTWEBSITE">
                 <ItemTemplate>
                   <asp:HyperLink ID="hlWebsite" runat="server" Target="_blank" HeaderText="Web Site" NavigateUrl='<%# Eval("ID","SUPPORTWEBSITE" %>' Text = '<%# Eval("SUPPORTWEBSITE") %>' />
                 </ItemTemplate> 
                </asp:TemplateField>

Open in new window

0
 

Author Comment

by:jknj72
ID: 40190986
"So, on your database, SUPPORTWEBSITE is a columnName and it has a value of www.abc123.com?"
That is correct....

Results....
NavigateUrl='<%# Eval("ID","SUPPORTWEBSITE") %>

This is what I had earlier and like I said its pointing to the Column Name again (SUPPORTWEBSITE) instead of the value www.abc123.com???
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40191100
Oh yea. That's true; that won't work.

Since you have already have the url stored on the database, then you really don't need all the navigateURL stuff.

Simply use this:

                   <a href='<%#Eval("owner")%>'/>Text</a>

as in:

                <asp:TemplateField HeaderText="Web Site" SortExpression="SUPPORTWEBSITE">
                 <ItemTemplate>
                     <a Target="_blank" href='<%#Eval("SUPPORTWEBSITE")%>'/><%#Eval("SUPPORTWEBSITE")%</a>                 
                </ItemTemplate> 

Open in new window

               </asp:TemplateField>
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40191111
Small error there:

 <asp:TemplateField HeaderText="Web Site" SortExpression="SUPPORTWEBSITE">
                 <ItemTemplate>
                     <a Target="_blank" href='<%#Eval("SUPPORTWEBSITE")%>'><%#Eval("SUPPORTWEBSITE")%</a>                 
                </ItemTemplate> 

Open in new window

0
 

Author Comment

by:jknj72
ID: 40191134
Getting an error that I was getting in the beginning. Seems to be requesting the website but with the folder my website before the url?

Error:
Requested URL: /Trans/www.assetworks.com/fa
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40191169
What error are you getting exactly?

Also, on your database, how is the url stored?

Is it stored on same folder as the aspx file your gridview is on?

In other words, is the url stored on Trans folder?

If yes, are rest of your aspx files stored on same folder?
0
 

Author Comment

by:jknj72
ID: 40191197
Q: What error are you getting exactly?
A: Server Error in '/Trans' Application.
--------------------------------------------------------------------------------
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Trans/www.abc123.com/fa

 Q: Also, on your database, how is the url stored?
A: The url is stored as "www.abc123.com"

Q: Is it stored on same folder as the aspx file your gridview is on?
A: Is what stored in the same folder? The url is stored in Oracle in a table and I bring it back via a stored proc and load into gridview

Q: In other words, is the url stored on Trans folder?
A: No it is a value in a column in a table

Q: If yes, are rest of your aspx files stored on same folder?
A:  N/A but just so you know all of my aspx files are in the root folder of my web site


Quick question. Is there something that will strip the '/Trans/'
 from '/Trans/www.abc123.com/fa'

Also, how come when I click on one of my other links that start with http or https  like this 'https://esupport.com' it works fine. It only doesnt work if the www is there?


Let me know if you have any other questions.  Thanks
0
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 total points
ID: 40191258
If you are storing in the db, I think you will to store with http://www.

In the meantime, can you try this:

       
         <a Target="_blank" href='<%# Eval("SUPPORTWEBSITE", "Trans/http://{0}.jpg")%>'><%#Eval("SUPPORTWEBSITE")%></a>

Open in new window


If that doesn't work, then try this:

         <a Target="_blank" href='<%# Eval("SUPPORTWEBSITE", "Trans/{0}.jpg")%>'><%#Eval("SUPPORTWEBSITE")%></a>

Open in new window

0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 40191264
Sorry wrong code, I was just testing:

It should be:

                   <a Target="_blank" href='<%# Eval("SUPPORTWEBSITE", "Trans/{0}")%>'><%#Eval("SUPPORTWEBSITE")%></a>

Open in new window

0
 

Author Comment

by:jknj72
ID: 40194316
FYI, the Trans/ is part of the problem. I dont need to put it in front of the {0} especially since it has been part of the problem from the beginning. I want to just bring up the site, which is the value in the column. I dont want anything before it and I dont want anything after it, I just want to bring up the site, which is the value in the column. The value in the column is www.abc123.com and that is what I want to bring up, not Trans/www.abc123.com. I just want to bring up www.abc123.com
0
 

Author Comment

by:jknj72
ID: 40194341
I dont have time to try and figure out how to open the www so I fortunately have the ability to change the data for the website field so thats what I did. I didnt wanna have to do this unless it was the only way for this to work but I can deal with this..
0
 

Author Closing Comment

by:jknj72
ID: 40194343
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question