Solved

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

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

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Built-In Report Creator / Viewer 5 41
Web page design problem 3 41
async questions 5 49
Unable to Install .NET 3.5 on Windows 2012 server 4 16
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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