javascript 如何从代码隐藏中调用 IF 条件下的确认框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16393542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call Confirmation box in IF condition from Code-Behind?
提问by SANDEEP
I'm using a LinkButton and a DropDown.
我正在使用 LinkButton 和 DropDown。
When I click on the LinkButton the DropDown appears.
当我单击 LinkButton 时,会出现 DropDown。
After selecting a DropDown value, I want a confirmation box called from JavaScript to appear, ensuring that the value is changed.
选择 DropDown 值后,我希望出现一个从 JavaScript 调用的确认框,确保该值已更改。
I'm calling this script in the second if
condition, but it's not working.
我在第二种if
情况下调用此脚本,但它不起作用。
After the confirmation I want to change the other value and exit from the condition.
确认后我想更改另一个值并退出条件。
protected void lnkbtnSave_Click(object sender, EventArgs e)
{
if ((ddlHiringManager.SelectedItem != null &&
(ddlHiringManager.SelectedItem.Text != lblHiringManager.Text)) &&
(Convert.ToInt32(ddlHiringManager.SelectedValue)) != -1)
{
if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))
{
ClsClientManager objClientManager = new ClsClientManager();
if (objClientManager.UpdateHRManagerByReqID(Convert.ToInt32(hdnReqId.Value), Convert.ToInt32(ddlHiringManager.SelectedValue)) > 0)
{
lblShowHiringManager.Text = ddlHiringManager.SelectedItem.Text;
}
}
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Please Select Hiring Manager !');</script>");
}
}
回答by ManojRK
You cannot use the result of RegisterStartupScript
method.
您不能使用RegisterStartupScript
方法的结果。
Change ASPX page code for the LinkButton as given below
更改 LinkButton 的 ASPX 页面代码,如下所示
<asp:LinkButton ID="lnkbtnSave" runat="server" OnClick="lnkbtnSave_Click"
OnClientClick="javascript: return confirm('Are you sure you want to change Hiring Manager for this requirement.');">Save</asp:LinkButton>
I have added the client side click event.
我已经添加了客户端点击事件。
On clicking the LinkButton you will get the confirmation box. The page will postback only if you click OK
in the confirmation box.
单击 LinkButton 后,您将看到确认框。仅当您单击OK
确认框时,页面才会回发。
回答by Neeraj Dubey
Please refer this Code Snippet. On dropdown selected index change event
请参考此代码片段。在下拉选择的索引更改事件上
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "Are you sure, you want to upload leave ?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
}
And for Client Side declare that method.
并为客户端声明该方法。
<script type="text/javascript">
function ConfirmApproval(objMsg) {
if (confirm(objMsg)) {
$('#divUploadLeave').fadeTo('slow', .6);
return true;
} else {
$('#divUploadLeave').fadeTo('slow', 1);
return false;
}
}
Hope It helps you.
希望对你有帮助。
Still if you want all things on Client Side please let me know.
不过,如果您想要客户端的所有内容,请告诉我。
回答by mmpatel009
Please add return before Confirm
this will solve your issue.
请在Confirm
此之前添加返回来解决您的问题。
**if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>return Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))**