无法在ASP.NET WebMethod中重定向URL

时间:2017-10-25 16:31:03

标签: c# asp.net redirect webmethod pagemethods

我在前端的ASP.NET(WebForms)应用程序中使用PageMethods来调用后端的c#函数,该函数从我的数据库中删除一行。删除部分可以工作,但我无法将当前.aspx文件中的url后缀重定向到名为StartPage.aspx的新文件。基本上,我想在按下按钮后将更新从UpdateNetEvent.aspx?ID = [Number]更改为StartPage.aspx。

我的按钮HTML如下:

<asp:Button ID="eventFormResolveBtn" runat="server" Text="Resolve NetEvent" OnClientClick="resolveClicked()" style="background-color:forestgreen;"/>

相应的Javascript函数调用是:

function resolveClicked() {

            try {

                var currentRowID = document.getElementById('<%=eventFormCurrentRowID.ClientID%>').value;

                PageMethods.resolveData(currentRowID, OnSucceeded, OnFailed);

                function OnSucceeded(result) {
                    window.location = result.d;
                }

                function OnFailed(error) {
                    alert("An error occured on resolveClicked()");
                }
            }
            catch (err) {
                return err;
            }
            return false;
        }

此外,后端的WebMethod调用如下:

[WebMethod]
    public static string resolveData(string CurrentRowID)
    {
       try
        {
            SqlCommand command;
            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
            command = new SqlCommand("deleteNetEventRow", sqlConn);

            command.Parameters.AddWithValue("@RowID", Convert.ToInt32(CurrentRowID));

            command.CommandType = System.Data.CommandType.StoredProcedure;
            sqlConn.Open();
            command.ExecuteNonQuery();
            sqlConn.Close();
            return "StartPage.aspx";
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR: " + ex.ToString());     // Add a log helper to display errors here
            return "Error in resolveData WebMethod";
        }
    }

SQLCommand调用和执行工作正常,但我无法重定向到另一个.aspx页面。

1 个答案:

答案 0 :(得分:0)

例如

window.location.href = "http://example.com/new_url";

您可以返回服务器的网址,如下所示:

result.d="http://localhost/StartPage.aspx"

function OnSucceeded(result) {
         window.location.href = result.d;
}