Solved

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

Posted on 2008-10-01
7
215 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 125 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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