[英]Unable to Redirect URL in ASP.NET WebMethod
我在前端的ASP.NET(WebForms)應用程序中使用PageMethods,以在后端調用ac#函數,該函數從數據庫中刪除一行。 刪除部分有效,但是我無法將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頁面后綴。
例如
window.location.href = "http://example.com/new_url";
您可以返回服務器的網址,如下所示:
result.d="http://localhost/StartPage.aspx"
function OnSucceeded(result) {
window.location.href = result.d;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.