I'm trying to get the confirm_value from TestConfirmValue() method in the code behind but when the javascript function callCheckMethod() is called, the last line of the function
alert("<%= TestConfirmValue() %>") is called before anything else. So, TestConfirmValue() is called first and confirm_value is always null.
How can I get the confirm_value to be set from the javascript function first before TestConfirmValue() is called?
On Code behind:
protected override void OnPreRender(EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "Javascript", "callCheckMethod();", false);
}
On aspx page:
function callCheckMethod() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Are you sure?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
alert("<%= TestConfirmValue() %>");
}
//Code Behind
public string TestConfirmValue()
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == null) return null;
if (confirmValue=="Yes")
{
//do something
}
return string.Empty;
}