problem with redirecting page with OnClientClick

Discussion in 'ASP .Net' started by Dan, Feb 12, 2007.

  1. Dan

    Dan Guest

    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>
    Dan, Feb 12, 2007
    #1
    1. Advertising

  2. 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>
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 12, 2007
    #2
    1. Advertising

  3. 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>
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 12, 2007
    #3
  4. Dan

    Dan Guest

    thanks

    "Peter Bromberg [C# MVP]" <> schreef in
    bericht news:...
    > 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>
    >>
    >>
    >>
    Dan, Feb 12, 2007
    #4
  5. Re: problem with redirecting page with OnClientClick

    On Feb 12, 5:48 pm, Peter Bromberg [C# MVP]
    <> 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();" />
    Alexey Smirnov, Feb 12, 2007
    #5
  6. Re: problem with redirecting page with OnClientClick

    On Feb 12, 6:03 pm, "Alexey Smirnov" <> wrote:
    >
    > I'd recommend to move js above and add "return false"
    >


    Well, Peter mentioned it already :)
    Alexey Smirnov, Feb 12, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex D.

    about onclientclick and postback

    Alex D., Sep 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    13,837
    Alex D.
    Sep 9, 2005
  2. Florian Doyon

    OnClientClick 'return' disables validation

    Florian Doyon, Sep 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,457
    Florian Doyon
    Sep 15, 2005
  3. Nathan Sokalski
    Replies:
    3
    Views:
    7,498
    Teemu Keiski
    Aug 1, 2007
  4. Nathan Sokalski

    Problem with ImageButton.OnClientClick when DataBinding

    Nathan Sokalski, Jul 31, 2007, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    692
    Teemu Keiski
    Aug 1, 2007
  5. rum23

    asp:ImageButton and onClientClick and page refresh

    rum23, Nov 28, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    588
Loading...

Share This Page