How can i redirect from a webmethod?

Discussion in 'ASP .Net' started by omer013, Jan 21, 2008.

  1. omer013

    omer013 Guest

    Hi;
    I have an aspx page with a WebMethod to redirect the user to another page.
    The Default.aspx.cs is;

    public partial class _Default : System.Web.UI.Page
    {
    [WebMethod]
    public static void redirect_user()
    {
    HttpContext.Current.Response.Redirect("home.aspx");
    }
    }

    and Default.aspx file;

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">

    <script type="text/javascript">

    function Button1_onclick()
    {
    PageMethods.redirect_user(redirectSuccess, redirectError);
    }

    function redirectSuccess(result)
    {
    alert(result);
    }

    function redirectError(error)
    {
    alert(error.get_message());
    }

    </script>

    <asp:ScriptManager ID="ScriptManager1" runat="server"
    EnablePageMethods="True" >
    </asp:ScriptManager>
    <div>
    <input id="Button1" type="button" value="button" onclick="return
    Button1_onclick()" />
    </div>
    </form>
    </body>
    </html>

    When i use HttpContext.Current.Response.Redirect("home.aspx"); i have the
    "Thread was being aborted" error. And if i use the
    HttpContext.Current.Response.Redirect("home.aspx", false); overload i have
    the "Authentication failed" error. So how can i redirect from a webmethod?
    omer013, Jan 21, 2008
    #1
    1. Advertising

  2. omer013

    bruce barker Guest

    you can not redirect the client from a web method. the redirect only
    redirects the method call. your web method should return the redirect url,
    and javascript can do the redirect.


    document.location.href = redirectUrl;


    -- bruce (sqlwork.com)


    "omer013" wrote:

    > Hi;
    > I have an aspx page with a WebMethod to redirect the user to another page.
    > The Default.aspx.cs is;
    >
    > public partial class _Default : System.Web.UI.Page
    > {
    > [WebMethod]
    > public static void redirect_user()
    > {
    > HttpContext.Current.Response.Redirect("home.aspx");
    > }
    > }
    >
    > and Default.aspx file;
    >
    > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > Inherits="_Default" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head runat="server">
    > <title>Untitled Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    >
    > <script type="text/javascript">
    >
    > function Button1_onclick()
    > {
    > PageMethods.redirect_user(redirectSuccess, redirectError);
    > }
    >
    > function redirectSuccess(result)
    > {
    > alert(result);
    > }
    >
    > function redirectError(error)
    > {
    > alert(error.get_message());
    > }
    >
    > </script>
    >
    > <asp:ScriptManager ID="ScriptManager1" runat="server"
    > EnablePageMethods="True" >
    > </asp:ScriptManager>
    > <div>
    > <input id="Button1" type="button" value="button" onclick="return
    > Button1_onclick()" />
    > </div>
    > </form>
    > </body>
    > </html>
    >
    > When i use HttpContext.Current.Response.Redirect("home.aspx"); i have the
    > "Thread was being aborted" error. And if i use the
    > HttpContext.Current.Response.Redirect("home.aspx", false); overload i have
    > the "Authentication failed" error. So how can i redirect from a webmethod?
    >
    >
    bruce barker, Jan 21, 2008
    #2
    1. Advertising

  3. omer013

    omer013 Guest

    Is there any way to redirect the page on the server side?

    "bruce barker" wrote:

    > you can not redirect the client from a web method. the redirect only
    > redirects the method call. your web method should return the redirect url,
    > and javascript can do the redirect.
    >
    >
    > document.location.href = redirectUrl;
    >
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "omer013" wrote:
    >
    > > Hi;
    > > I have an aspx page with a WebMethod to redirect the user to another page.
    > > The Default.aspx.cs is;
    > >
    > > public partial class _Default : System.Web.UI.Page
    > > {
    > > [WebMethod]
    > > public static void redirect_user()
    > > {
    > > HttpContext.Current.Response.Redirect("home.aspx");
    > > }
    > > }
    > >
    > > and Default.aspx file;
    > >
    > > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > > Inherits="_Default" %>
    > >
    > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    > > <html xmlns="http://www.w3.org/1999/xhtml">
    > > <head runat="server">
    > > <title>Untitled Page</title>
    > > </head>
    > > <body>
    > > <form id="form1" runat="server">
    > >
    > > <script type="text/javascript">
    > >
    > > function Button1_onclick()
    > > {
    > > PageMethods.redirect_user(redirectSuccess, redirectError);
    > > }
    > >
    > > function redirectSuccess(result)
    > > {
    > > alert(result);
    > > }
    > >
    > > function redirectError(error)
    > > {
    > > alert(error.get_message());
    > > }
    > >
    > > </script>
    > >
    > > <asp:ScriptManager ID="ScriptManager1" runat="server"
    > > EnablePageMethods="True" >
    > > </asp:ScriptManager>
    > > <div>
    > > <input id="Button1" type="button" value="button" onclick="return
    > > Button1_onclick()" />
    > > </div>
    > > </form>
    > > </body>
    > > </html>
    > >
    > > When i use HttpContext.Current.Response.Redirect("home.aspx"); i have the
    > > "Thread was being aborted" error. And if i use the
    > > HttpContext.Current.Response.Redirect("home.aspx", false); overload i have
    > > the "Authentication failed" error. So how can i redirect from a webmethod?
    > >
    > >
    omer013, Jan 23, 2008
    #3
  4. omer013

    omer013 Guest

    Is there any way to redirect on the server side?

    "bruce barker" wrote:

    > you can not redirect the client from a web method. the redirect only
    > redirects the method call. your web method should return the redirect url,
    > and javascript can do the redirect.
    >
    >
    > document.location.href = redirectUrl;
    >
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "omer013" wrote:
    >
    > > Hi;
    > > I have an aspx page with a WebMethod to redirect the user to another page.
    > > The Default.aspx.cs is;
    > >
    > > public partial class _Default : System.Web.UI.Page
    > > {
    > > [WebMethod]
    > > public static void redirect_user()
    > > {
    > > HttpContext.Current.Response.Redirect("home.aspx");
    > > }
    > > }
    > >
    > > and Default.aspx file;
    > >
    > > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > > Inherits="_Default" %>
    > >
    > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    > > <html xmlns="http://www.w3.org/1999/xhtml">
    > > <head runat="server">
    > > <title>Untitled Page</title>
    > > </head>
    > > <body>
    > > <form id="form1" runat="server">
    > >
    > > <script type="text/javascript">
    > >
    > > function Button1_onclick()
    > > {
    > > PageMethods.redirect_user(redirectSuccess, redirectError);
    > > }
    > >
    > > function redirectSuccess(result)
    > > {
    > > alert(result);
    > > }
    > >
    > > function redirectError(error)
    > > {
    > > alert(error.get_message());
    > > }
    > >
    > > </script>
    > >
    > > <asp:ScriptManager ID="ScriptManager1" runat="server"
    > > EnablePageMethods="True" >
    > > </asp:ScriptManager>
    > > <div>
    > > <input id="Button1" type="button" value="button" onclick="return
    > > Button1_onclick()" />
    > > </div>
    > > </form>
    > > </body>
    > > </html>
    > >
    > > When i use HttpContext.Current.Response.Redirect("home.aspx"); i have the
    > > "Thread was being aborted" error. And if i use the
    > > HttpContext.Current.Response.Redirect("home.aspx", false); overload i have
    > > the "Authentication failed" error. So how can i redirect from a webmethod?
    > >
    > >
    omer013, Jan 23, 2008
    #4
  5. omer013

    Ven Guest

    Hi,

    Have you got the answer ? am also in same place, i want to redirect to
    another from webmethod...

    "omer013" wrote:

    > Is there any way to redirect on the server side?
    >
    > "bruce barker" wrote:
    >
    > > you can not redirect the client from a web method. the redirect only
    > > redirects the method call. your web method should return the redirect url,
    > > and javascript can do the redirect.
    > >
    > >
    > > document.location.href = redirectUrl;
    > >
    > >
    > > -- bruce (sqlwork.com)
    > >
    > >
    > > "omer013" wrote:
    > >
    > > > Hi;
    > > > I have an aspx page with a WebMethod to redirect the user to another page.
    > > > The Default.aspx.cs is;
    > > >
    > > > public partial class _Default : System.Web.UI.Page
    > > > {
    > > > [WebMethod]
    > > > public static void redirect_user()
    > > > {
    > > > HttpContext.Current.Response.Redirect("home.aspx");
    > > > }
    > > > }
    > > >
    > > > and Default.aspx file;
    > > >
    > > > <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    > > > Inherits="_Default" %>
    > > >
    > > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > > > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    > > > <html xmlns="http://www.w3.org/1999/xhtml">
    > > > <head runat="server">
    > > > <title>Untitled Page</title>
    > > > </head>
    > > > <body>
    > > > <form id="form1" runat="server">
    > > >
    > > > <script type="text/javascript">
    > > >
    > > > function Button1_onclick()
    > > > {
    > > > PageMethods.redirect_user(redirectSuccess, redirectError);
    > > > }
    > > >
    > > > function redirectSuccess(result)
    > > > {
    > > > alert(result);
    > > > }
    > > >
    > > > function redirectError(error)
    > > > {
    > > > alert(error.get_message());
    > > > }
    > > >
    > > > </script>
    > > >
    > > > <asp:ScriptManager ID="ScriptManager1" runat="server"
    > > > EnablePageMethods="True" >
    > > > </asp:ScriptManager>
    > > > <div>
    > > > <input id="Button1" type="button" value="button" onclick="return
    > > > Button1_onclick()" />
    > > > </div>
    > > > </form>
    > > > </body>
    > > > </html>
    > > >
    > > > When i use HttpContext.Current.Response.Redirect("home.aspx"); i have the
    > > > "Thread was being aborted" error. And if i use the
    > > > HttpContext.Current.Response.Redirect("home.aspx", false); overload i have
    > > > the "Authentication failed" error. So how can i redirect from a webmethod?
    > > >
    > > >
    Ven, Mar 18, 2008
    #5
    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. John
    Replies:
    2
    Views:
    593
  2. Sal
    Replies:
    1
    Views:
    401
  3. Ryan Bingham

    Can't return class instance from WebMethod

    Ryan Bingham, Aug 11, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    110
    Ryan Bingham
    Aug 11, 2003
  4. DaberElay
    Replies:
    0
    Views:
    127
    DaberElay
    Oct 5, 2004
  5. Guest
    Replies:
    1
    Views:
    162
    Brock Allen
    May 25, 2005
Loading...

Share This Page