【发布时间】:2017-10-25 16:31:03
【问题描述】:
我在前端的 ASP.NET (WebForms) 应用程序中使用 PageMethods 来调用后端的 c# 函数,该函数从我的数据库中删除一行。删除部分有效,但我无法将 url 后缀从我当前的 .aspx 文件重定向到一个名为 StartPage.aspx 的新文件。本质上,我想在按下按钮后将 URL 从 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 页面。
【问题讨论】:
-
您遇到错误了吗?发生什么了?你检查
result.d了吗? -
当我在 chrome 中调试时 result.d 得到了正确的字符串值并进行了调用,但之后没有任何反应,它位于同一页面上
标签: c# asp.net redirect webmethod pagemethods