【发布时间】:2017-06-20 05:38:29
【问题描述】:
我的页面中有一个按钮,如果条件匹配,它的点击事件会执行某些代码,否则它会要求确认是否继续。我有一个用于确认的 javascript 函数,如下所示。
<script type="text/javascript">
function getConfirmation(){
var retVal = confirm("Do you want to continue ?");
if( retVal == true ){
return true;
}
else{
return false;
}
}
</script>
<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" onClientClick="getConfirmation()"></button>
后面的代码是这样的:
void lnkBtn_Click(Object sender, EventArgs e)
{
if (txtMyText.Text!="")
{
///need confirmation here whether to continue.
}
else
{
//continue with normal code.
}
}
现在的问题是即使在不满足条件的情况下单击按钮也会触发 confirm()。我希望 confirm() 仅在满足条件时触发。请帮我做这件事。
谢谢你的期待。
我尝试了这两种解决方案,但似乎我在某处犯了错误。我附上下面的完整代码。请求您帮助我找出我错在哪里以及如何解决它。
<script type="text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("This will completely delete the project. Are you sure?")) {
confirm_value.value = "Yes";
}
else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" onClientClick="getConfirmation()"></button>
代码背后:
protected void ibExport_Click(object sender, ImageClickEventArgs e)
{
string str=gdView.HeaderRow.Cells[8].Text;
System.Web.UI.WebControls.TextBox txtID = (System.Web.UI.WebControls.TextBox)gdView.Rows[0].Cells[8].FindControl(str);
if (txtID.Text != "")
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
MyAlert("Yes clicked");
}
else
{
MyAlert("No clicked");
}
}
else
{
MyAlert("No Text found.");
}
}
使用txtID.Text !=" "点击ibExport时,不显示确认对话框。而是直接弹出“未点击”警报。
【问题讨论】:
标签: javascript c# asp.net