问题
I want to show the confirmation box in asp.net from server side:
I want to call it from server side because I have to take the customer message from server side. I am using the below code
string str = "Are you sure, you want to Approve this Record?";
ClientScript.RegisterStartupScript(typeof(Page), "Popup", "return confirm('" + str + "');",true);
// More code ....
Now it is showing the popup and irrespective of what I click, whether "ok" or "Cancel", my code is executing.
Please let me know how can I restrict code to be executed when user select "cancel" in the confirmation box.
回答1:
Check it out:
CODE BEHIND:
string str = "Are you sure, you want to Approve this Record?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
ASPX:
function ConfirmApproval(objMsg)
{
if(confirm(objMsg))
{
alert("execute code.");
return true;
}
else
return false;
}
回答2:
check following example code.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btnOK" runat="server" Text="OK" onclick="btnOK_Click" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="lblID" PopupControlID="pnlConfirmation">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlConfirmation" runat="server" Style="background-color: #D4D0C8;
border: 2px solid black; width: 300px; height: 100px;">
<asp:Label ID="lblID" runat="server"></asp:Label>
<table cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>
Your confirmation message here.
</td>
</tr>
<tr>
<td align="center">
<asp:Button ID="btnOK1" runat="server" Text="OK" onclick="btnOK1_Click" /> <asp:Button ID="btncancel"
runat="server" Text="Cancel" onclick="btncancel_Click" />
</td>
</tr>
</table>
</asp:Panel>
On server side.....
protected void btnOK_Click(object sender, EventArgs e)
{
//some code here.
//then show modal popup
ModalPopupExtender1.Show();
}
protected void btnOK1_Click(object sender, EventArgs e)
{
//If ok some code here to execute
ModalPopupExtender1.Hide();
}
protected void btncancel_Click(object sender, EventArgs e)
{
//hide modal popup
ModalPopupExtender1.Hide();
}
http://forums.asp.net/t/1499789.aspx?confirmation+popup+from+server+side
来源:https://stackoverflow.com/questions/5114607/confirmation-box-from-server-side-in-asp-net