Solved

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

Posted on 2008-10-01
7
213 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
  • 4
  • 3
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
Comment Utility
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
Comment Utility
Like this:

                    while (result.Read())

                    {

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

                    }

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
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
Comment Utility
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 125 total points
Comment Utility
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
Comment Utility
Thanks! It worked with "return false;"
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now