2015-04-02 104 views
1

我已經和阿賈克斯和C#方法阿賈克斯重定向到另一個頁面......C#,通過靜態方法

我需要重定向到靜態Web方法的另一頁..

但我不能這樣做。

方法重定向到另一頁是follwoing

if (tempUser == "0") 
       { 

        HttpContext.Current.Response.Redirect("login.aspx"); 
       } 
       else 
       { 
        HttpContext.Current.Response.Redirect("list.aspx"); 
       } 

AJAX

$('#Button1').click(function() { 
        var HTML = document.getElementById("data").innerHTML; 
       // alert(HTML); 
       var Fname = document.getElementById("MyText").value; 
       Senddata = { "HTML": HTML, "Fname": Fname }; 
       $.ajax({ 
        type: "Post", 
        url: "/wwwroot/Default.aspx/save", 
        data: JSON.stringify(Senddata), 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (result) { 


        } 
       }); 
      } 
     }); 

C#靜態Web方法捕獲集團拋出

[WebMethod(EnableSession = true)] 
    public static void save(string HTML, string Fname) 
    { 


     HttpContext.Current.Session["GUID"] = Convert.ToString(Guid.NewGuid()); 

     try 
     { 
      SqlConnection con = new SqlConnection(); 
      con.ConnectionString = ConfigurationManager.ConnectionStrings["MS"].ConnectionString; 
      SqlCommand cmd = new SqlCommand(); 
      if (con.State == ConnectionState.Closed) 
      { 
       con.Open(); 
      } 

      string tempUser = Convert.ToString(HttpContext.Current.Session["UserID"]); 

      if (string.IsNullOrEmpty(tempUser)) 
      { 
       tempUser = "0"; 
      } 

      cmd.CommandType = CommandType.Text; 
      cmd.Connection = con; 
      cmd.CommandText = "insert into Forms(Name,HTML,UserID,GUID)values(@Name,@HTML,@UserID,@GUID)"; 
      cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 1024).Value = Fname; 
      cmd.Parameters.Add("@HTML", SqlDbType.VarChar, 8000).Value = HTML.Trim(); 
      cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = tempUser; 
      cmd.Parameters.Add("@GUID", SqlDbType.NVarChar, 50).Value = HttpContext.Current.Session["GUID"]; 

      cmd.ExecuteNonQuery(); 
      cmd.Dispose(); 
      con.Close(); 
      if (tempUser == "0") 
      { 

       HttpContext.Current.Response.Redirect("login.aspx"); 
      } 
      else 
      { 
       HttpContext.Current.Response.Redirect("list.aspx"); 
      } 


     } 
     catch (Exception ex) 
     { 

      CommonBLL.WriteExceptionLog(ex, "Form Save Default.aspx"); 
      throw ex; 
     } 

    } 
} 

錯誤k是

[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} 

回答

1

我認爲,期間或之後,你無法從服務器端代碼重定向阿賈克斯呼籲。 至少你可以返回一個url(例如Response.write(「YourURlBuildOnC#」))用於重定向成功函數

+0

是的,你是ccorrect ..我解決了我的問題,通過更改靜態字符串並返回值爲ajax函數 – 2015-04-06 20:08:34

1

您在Ajax調用成功功能重定向:

$('#Button1').click(function() { 
    var HTML = document.getElementById("data").innerHTML; 
    // alert(HTML); 
    var Fname = document.getElementById("MyText").value; 
    Senddata = { "HTML": HTML, "Fname": Fname }; 
    $.ajax({ 
     type: "Post", 
      url: "/wwwroot/Default.aspx/save", 
      data: JSON.stringify(Senddata), 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (result) { 
       window.location.href = "http://stackoverflow.com"; 
      } 
     }); 
    } 
}); 

希望這有助於enter code here

+0

感謝您的回答,我想通過C#代碼重定向後面.. – 2015-04-03 14:26:22