Hi I want to call a client side javascript confirm message from code behind in asp.
I want to use the true or false return value from the confirm message.
I'm doing like this, but this is not the correct way, please tell me how can I do the same.
ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "confirm('No Rule Type Ids found for This Rule.');", true);
Hi I want to call a client side javascript confirm message from code behind in asp.
I want to use the true or false return value from the confirm message.
I'm doing like this, but this is not the correct way, please tell me how can I do the same.
ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "confirm('No Rule Type Ids found for This Rule.');", true);
Share
Improve this question
asked Apr 7, 2014 at 11:15
user3475314user3475314
751 gold badge3 silver badges8 bronze badges
2
- I prefer to use typeof(MyPage) instead of GetType() saves the reflection (unless you're on a base page obviously) – Liath Commented Apr 7, 2014 at 11:23
- I think its best that you add confirmation in html part onclientclick="return confirm('Are you sure, you want to continue?'); – raw_hitt Commented Aug 26, 2021 at 5:48
6 Answers
Reset to default 12I think This is what you want to achieve:
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
.aspx code
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
C#
public void OnConfirm(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
}
Instead of directly writing confirm in the code behind. Write the name of javascript function. Forexample,
ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "OpenConfirmDialog();", true);
In your javascript, write the function OpenConfirmDialog
<script>
function OpenConfirmDialog()
{
if (confirm('No Rule Type Ids found for This Rule.'))
{
//True .. do something
}
else
{
//False .. do something
}
}
</script>
You can't mix client-side code with server-side code. The client side code (javascript) will not be sent to the client (browser) until the server-side code has pleted.
You will need to stop processing and show the question to the user (maybe by a redirect to some other page), and then (on confirm) continue with your processing.
Response.Write("<script language=javascript>");
Response.Write("if(confirm('El vehiculo no existe, Deseas crear el registro?')){window.location.href='IngresoVehiculos.aspx'}");
Response.Write("</script>");
use:
Response.Write("<script>alert('Open Message!');</script>");
You can do it without using Javascript function
Try
if (MessageBox.Show("confirm('No Rule Type Ids found for This Rule.')",
"myconfirm",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// yes
}
else
{
//No
}