用的vs2003,做了一个页面,要实现点击一个按钮,先判断某个表达式,如果表达式不通过,就弹出一个confirm确认,通过判断点击confirm对话框的确认还是取消来看是否继续执行后续的语句。
为了实现这个功能我添加了一个hidden input。前台中:
两按钮,和两Label(Label用于检测点击效果)
<input type="hidden" runat="server" id="iptBtn">
<asp:Button id="btn1" runat="server" Text="Test"></asp:Button>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:Label id="Label2" runat="server"></asp:Label
<script language="javascript">
function SureToClose()
{
var rtnVal = document.getElementById("ipBtn");
if(confirm("请确认是否仍要关闭?")==true)
rtnVal.value = "1";
else
rtnVal.value = "0"; </script>
后台cs中: private void btn1_Click(object sender, System.EventArgs e)
{
if(3>4) //这里设为固定条件用于调试
{
string strScript = "<script language = JavaScript>";
strScript += "SureToClose();";
strScript += "</script>";
if (!this.IsStartupScriptRegistered("Startup"))
{
this.RegisterStartupScript("Startup", strScript);
}
}
if(this.iptBtn.Value==("1"))
{
this.Label1.Text="测试结果1";
this.Label2.Text="测试结果1";
}
else
{
this.Label1.Text=this.iptBtn.Value;
this.Label2.Text="测试结果2";
}
}
点击btn1后,确实弹出了confirm对话框,但是弹出后还未点击确定还是取消(confirm对话框还在桌面上),发现页面的Label2已经显示为“测试结果2”,Label1无内容,此时页面的状态栏是显示Load到一半的,点击confirm对话框的确认或取消后,页面状态为完成,页面显示内容与点击confirm对话框的确认或取消前一样。请问:
1、为什么iptBtn.Value的值会没有,是否是没有成功赋到iptBtn.Value的值?
2、针对上述的需求,请问需要怎么修改?
为了实现这个功能我添加了一个hidden input。前台中:
两按钮,和两Label(Label用于检测点击效果)
<input type="hidden" runat="server" id="iptBtn">
<asp:Button id="btn1" runat="server" Text="Test"></asp:Button>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:Label id="Label2" runat="server"></asp:Label
<script language="javascript">
function SureToClose()
{
var rtnVal = document.getElementById("ipBtn");
if(confirm("请确认是否仍要关闭?")==true)
rtnVal.value = "1";
else
rtnVal.value = "0"; </script>
后台cs中: private void btn1_Click(object sender, System.EventArgs e)
{
if(3>4) //这里设为固定条件用于调试
{
string strScript = "<script language = JavaScript>";
strScript += "SureToClose();";
strScript += "</script>";
if (!this.IsStartupScriptRegistered("Startup"))
{
this.RegisterStartupScript("Startup", strScript);
}
}
if(this.iptBtn.Value==("1"))
{
this.Label1.Text="测试结果1";
this.Label2.Text="测试结果1";
}
else
{
this.Label1.Text=this.iptBtn.Value;
this.Label2.Text="测试结果2";
}
}
点击btn1后,确实弹出了confirm对话框,但是弹出后还未点击确定还是取消(confirm对话框还在桌面上),发现页面的Label2已经显示为“测试结果2”,Label1无内容,此时页面的状态栏是显示Load到一半的,点击confirm对话框的确认或取消后,页面状态为完成,页面显示内容与点击confirm对话框的确认或取消前一样。请问:
1、为什么iptBtn.Value的值会没有,是否是没有成功赋到iptBtn.Value的值?
2、针对上述的需求,请问需要怎么修改?
在弹出对话框之前,下面那些代码都执行完了.
修改一下:
var rtnVal = document.getElementById("ipBtn").value;
请问<%=控件ID.ClientID%>具体是怎么写法?
{
if (4 > 3) //这里设为固定条件用于调试
{
string strScript = "<script language = JavaScript>";
strScript += "SureToClose();";
strScript += "</script>";
if (!this.IsStartupScriptRegistered("Startup"))
{
this.RegisterStartupScript("Startup", strScript);
}
}
if (this.iptBtn.Value == ("1"))
{
this.Label1.Text = "测试结果1";
this.Label2.Text = "测试结果1";
}
else //刚开始iptBtn的值为空,所以会执行下面的赋值。 下面Label1为空
{
this.Label1.Text = this.iptBtn.Value;
this.Label2.Text = "测试结果2";
} }当弹出确认对话框时,如果点击确定,则执行if(confirm("请确认是否仍要关闭?")==true) rtnVal.value = "1";
这样rtnVal的值为1.
这时再点击test按钮,因为rtnVal的值为1.所以会执行
if (this.iptBtn.Value == ("1"))
{
this.Label1.Text = "测试结果1";
this.Label2.Text = "测试结果1";
}
也就是说 交换的点击对话框的“确定”和“取消”,Label1和Label2的值就会在0 测试结果2 和测试结果1 测试结果1 变来变去
iptBtn.Value的值有成功赋到。
可以先设置它的默认值value=<input type="hidden" runat="server" id="iptBtn" value="1">
这样做的效果就和我默认点击确认是一样了,我想要的是第一次点击后就根据confirm对话框的返回结果来进行之后的程序。