Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > problem with redirecting page with OnClientClick

Reply
Thread Tools

problem with redirecting page with OnClientClick

 
 
Dan
Guest
Posts: n/a
 
      02-12-2007
Hi,

i try to redirect to another aspx page with jscript. I use "OnClientClick"
but nothing happens.
I tried two ways.
What am i doing wrong?
Thanks
Dan

....
<form id="form1" runat="server">
<asp:Button ID="ContinueButton" runat="server" OnClientClick="return
profiel()" />
</form>
....

<script language="javascript" type="text/javascript">
function profiel()
{
//window.location.href="page2.aspx"
Response.Redirect("page2.aspx")
}
</script>


 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      02-12-2007
Yes, the problem is that you are trying to use Response.Redirect (which is
server-side .NET code) in client-side script, which won't work. Use
location.href=newurl;

instead.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"Dan" wrote:

> Hi,
>
> i try to redirect to another aspx page with jscript. I use "OnClientClick"
> but nothing happens.
> I tried two ways.
> What am i doing wrong?
> Thanks
> Dan
>
> ....
> <form id="form1" runat="server">
> <asp:Button ID="ContinueButton" runat="server" OnClientClick="return
> profiel()" />
> </form>
> ....
>
> <script language="javascript" type="text/javascript">
> function profiel()
> {
> //window.location.href="page2.aspx"
> Response.Redirect("page2.aspx")
> }
> </script>
>
>
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      02-12-2007
P.S.
Also, you need to return false after the window.location.href assignment to
prevent your server-side event from firing; then it will work.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"Dan" wrote:

> Hi,
>
> i try to redirect to another aspx page with jscript. I use "OnClientClick"
> but nothing happens.
> I tried two ways.
> What am i doing wrong?
> Thanks
> Dan
>
> ....
> <form id="form1" runat="server">
> <asp:Button ID="ContinueButton" runat="server" OnClientClick="return
> profiel()" />
> </form>
> ....
>
> <script language="javascript" type="text/javascript">
> function profiel()
> {
> //window.location.href="page2.aspx"
> Response.Redirect("page2.aspx")
> }
> </script>
>
>
>

 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      02-12-2007
thanks

"Peter Bromberg [C# MVP]" <(E-Mail Removed)> schreef in
bericht news:(E-Mail Removed)...
> P.S.
> Also, you need to return false after the window.location.href assignment
> to
> prevent your server-side event from firing; then it will work.
> Peter
>
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> Short urls & more: http://ittyurl.net
>
>
>
>
> "Dan" wrote:
>
>> Hi,
>>
>> i try to redirect to another aspx page with jscript. I use
>> "OnClientClick"
>> but nothing happens.
>> I tried two ways.
>> What am i doing wrong?
>> Thanks
>> Dan
>>
>> ....
>> <form id="form1" runat="server">
>> <asp:Button ID="ContinueButton" runat="server"
>> OnClientClick="return
>> profiel()" />
>> </form>
>> ....
>>
>> <script language="javascript" type="text/javascript">
>> function profiel()
>> {
>> //window.location.href="page2.aspx"
>> Response.Redirect("page2.aspx")
>> }
>> </script>
>>
>>
>>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 5:48 pm, Peter Bromberg [C# MVP]
<(E-Mail Removed)> wrote:
> Yes, the problem is that you are trying to use Response.Redirect (which is
> server-side .NET code) in client-side script, which won't work. Use
> location.href=newurl;
>
> instead.
> Peter
>
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> Short urls & more: http://ittyurl.net
>
>
>
> "Dan" wrote:
> > Hi,

>
> > i try to redirect to another aspx page with jscript. I use "OnClientClick"
> > but nothing happens.
> > I tried two ways.
> > What am i doing wrong?
> > Thanks
> > Dan

>
> > ....
> > <form id="form1" runat="server">
> > <asp:Button ID="ContinueButton" runat="server" OnClientClick="return
> > profiel()" />
> > </form>
> > ....

>
> > <script language="javascript" type="text/javascript">
> > function profiel()
> > {
> > //window.location.href="page2.aspx"
> > Response.Redirect("page2.aspx")
> > }
> > </script>- Hide quoted text -

>


I'd recommend to move js above and add "return false"

Example:


<script language="javascript" type="text/javascript">
function profiel()
{
window.location.href="page2.aspx";
return false;

}
</script>

<asp:Button ID="ContinueButton" runat="server" OnClientClick="return
profiel();" />

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 6:03 pm, "Alexey Smirnov" <(E-Mail Removed)> wrote:
>
> I'd recommend to move js above and add "return false"
>


Well, Peter mentioned it already

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp:ImageButton and onClientClick and page refresh rum23 ASP .Net Web Controls 1 01-24-2009 01:25 PM
Problem with ImageButton.OnClientClick when DataBinding Nathan Sokalski ASP .Net 3 08-01-2007 05:44 PM
Problem with ImageButton.OnClientClick when DataBinding Nathan Sokalski ASP .Net Web Controls 3 08-01-2007 05:44 PM
OnClientClick 'return' disables validation Florian Doyon ASP .Net 0 09-15-2005 02:43 PM
about onclientclick and postback Alex D. ASP .Net 3 09-09-2005 03:17 PM



Advertisments