Solved

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

Posted on 2008-10-01
7
214 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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