Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open a new site by clicking a Button (Simulation of a hyperlink)

Posted on 2008-10-01
7
Medium Priority
?
220 Views
Last Modified: 2013-12-17
Hi,

I have a problem. I would like to configure a Button to redirect to another site by clicking it.
Just like a link.

How does ist work?

I tried it with following code, but it doesn't work!
Button1.Attributes.Add("onClick", "window.parent.location='" + result[3] + "'; window.location='" + result[3] + "';");

Open in new window

0
Comment
Question by:eb2007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 22614606
Hello eb2007,

When do you use this code, the .Attributes.Add allows you to add a client-side script function to the event. This can only be done before the elemnt is rendered. You cannot do this on postback for example as it will indeed have no effect. Additionally what is the value of result[3]? Is it available and correct at the time you add the attribute to the element? you also don't need to set both the parent and current location at the same time, in fact it is illogical to do so.

Regards,

TimCottee
0
 

Author Comment

by:eb2007
ID: 22614679
Hello TimCottee,

this line of code is called in the page_load() function. It should enrich a button with client-side properties.
result[3] is only an entry from the result-set of a database response object. The function should be enriched with links coming from a database.

Thanks!
0
 

Author Comment

by:eb2007
ID: 22614708
Like this:

                    while (result.Read())
                    {
                            Button1.Attributes.Add("onClick", "window.parent.location='" + result[3] + "'; window.location='" + result[3] + "';");
                    }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:TimCottee
ID: 22614771
eb2007,

Ok, are you sure that it even executes this line (presumably you are certain that the datareader has rows.

It seems strange to me that you are adding potentially many onclick attributes to the same button, this in itself may cause conflicts and as noted above if there are no rows iterated then you won't even get any added.

One way to narrow down where the problem is, is to skip this step and just have:

Button1.Attributes.Add("OnClick","window.location.href='http://www.experts-exchange.com';");

If when you click this button you get back to EE then you know that the mechanism works.

TimCottee
0
 

Author Comment

by:eb2007
ID: 22614973
But with

Button1.Attributes.Add("OnClick","window.location.href='http://www.experts-exchange.com';");

the same site refreshes and nothing happens. Not even a new site appears.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 22614999
eb2007,

Ok, then what is happening is that the page is being submitted which trumps the redirect:

Button1.Attributes.Add("OnClick","window.location.href='http://www.experts-exchange.com';return false;");

Should deal with that.

IF then you get to the new site on clicking the button you can then put this back into your loop and test the next stage. If that doesn't take you to a new page then you know that the issue is related to either the way you are assigning the new window.location.href or that this value is simply empty.

TimCottee
0
 

Author Closing Comment

by:eb2007
ID: 31501983
Thanks! It worked with "return false;"
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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