用的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、针对上述的需求,请问需要怎么修改?

解决方案 »

  1.   

    c# 里面的3>4 改为3<4
      

  2.   

    楼主,这是B/S程序,不是C/S程序.
    在弹出对话框之前,下面那些代码都执行完了.
      

  3.   

     var rtnVal = document.getElementById("ipBtn");                
    修改一下:
     var rtnVal = document.getElementById("ipBtn").value;                
      

  4.   

    document.getElementById("<%=控件ID.ClientID%>").value=1;
      

  5.   


    请问<%=控件ID.ClientID%>具体是怎么写法?
      

  6.   

     protected void btn1_Click(object sender, EventArgs e)
        {
            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";
            }
      

  7.   

    上面
    也就是说 交换的点击对话框的“确定”和“取消”,Label1和Label2的值就会在0 测试结果2 和测试结果1 测试结果1 变来变去
    iptBtn.Value的值有成功赋到。
      

  8.   

    回gdjlc,那我想要第一次点击就能直接获取 this.iptBtn.Value的值要怎么做呢?
      

  9.   


    可以先设置它的默认值value=<input type="hidden" runat="server" id="iptBtn" value="1"> 
      

  10.   


    这样做的效果就和我默认点击确认是一样了,我想要的是第一次点击后就根据confirm对话框的返回结果来进行之后的程序。
      

  11.   

    var rtnVal = document.getElementById("ipBtn"); 应该是获取不到那个隐藏的控件的,建议看下运行后的控件ID是什么