I am using PageMethods in my ASP.NET (WebForms) app on my front end to call a c# function on the back end that deletes a row from my database. The deleting part works but I am unable to redirect the url afterwords from my current .aspx file to a new one called StartPage.aspx. Essentially, I want to make it so after a button press the url changes from UpdateNetEvent.aspx?ID=[Number] to StartPage.aspx.
My HTML for the button is as follows:
<asp:Button ID="eventFormResolveBtn" runat="server" Text="Resolve NetEvent" OnClientClick="resolveClicked()" style="background-color:forestgreen;"/>
And the corresponding Javascript function call is:
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;
}
Additionally, the WebMethod call on the backend is as follows:
[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";
}
}
The SQLCommand call and execution works perfectly but I am not able to redirect to another .aspx page afterwords.
e.g
window.location.href = "http://example.com/new_url";
you can return the url of the server like below :
result.d="http://localhost/StartPage.aspx"
function OnSucceeded(result) {
window.location.href = result.d;
}