我原的按键加了一个确认窗口:
string strPrompt = ...;
btnApply.Attributes.Add("onclick","return confirm('" + strPrompt + "');"); btnApply服务器按钮还有服务器端的ON CLICK事件后来,我为了防止多次按下,改成了
btnApply.Attributes.Add("onclick","window.document.all('btnApply').disabled=true;if(confirm('" + strPrompt + "')){return true;} else{window.document.all('btnApply').disabled=false; return false;}");为什么我这个按键的服务器端的ONCLICK事件激发不了了?
多谢!

解决方案 »

  1.   

    Page_Load中:
    string strPrompt = "...";
    if (!IsPostBack)
    {
        btnApply.Attributes.Add("onclick","return _confirm(" + strPrompt + ");");
    }客户端:
    <script language="javascript">
    function _confirm(str)
    {
        window.document.all('btnApply').disabled = true;
        
        if (confirm(str))
        {
            return true;
        } 
        else
        {
            window.document.all('btnApply').disabled = false;
            return false;
        }
        
        return false;
    }
    </script>
      

  2.   

    在javascript中,非函数是不能有返回值的,按照你原来的写法,ie的左下角应该是有javascript的错误提示的。
      

  3.   

    先谢过,不过好象还是不行!
    和我原先的一样,
    如果点击取消,则被禁止的按钮又还原了,但点击确认后,按钮是被禁用了,可是我服务器端的点击代码btnApply_Click没有被触发? 真是晕~~
      

  4.   

    btnApply.Attributes.Add("onclick","window.document.all('btnApply').disabled=true;if(confirm('" + strPrompt + "')){return true;} else{window.document.all('btnApply').disabled=false; return false;};document.formName.submit();");//把页面提交了
      

  5.   

    就是说Submit!服务器段代码,必须提交后才能执行!
      

  6.   

    具体原理可以看MSDN上的WEB页的生命周期!赫赫,这个可能作ASP的人会理解的深刻一些!
      

  7.   

    我把document.Form1.submit();提到 return true 之前,能够照成刷新页面,
    但我的提交按钮是服务器的控件,不是HTML的submit按钮 
    所以现在提交到SERVER时只能进入Page Load, 进不到我的btnApply_Click
    请问怎么提交服务器端的按钮呢?
      

  8.   

    1、创建两个hidden变量
    <input type="hidden" name="__EVENTTARGET"> 
    <input type="hidden" name="__EVENTARGUMENT">2、javascript追加以下方法:
    function __doPostBack(eventTarget, eventArgument) 
    {
        var theform;
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
        {
        theform = document.forms["Form1"];
        }
        else 
        {
        theform = document.Form1;
        }
        
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTARGUMENT.value = eventArgument;
        theform.submit();
    }请确认以上内容是否已经存在,如果存在,就不用再追加了。把submit改为:
    __doPostBack("btnApply","");
      

  9.   

    好象还是不行,大侠
    aspx:function __doPostBack(eventTarget, eventArgument) 
    {
        var theform;
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
        {
        theform = document.forms["Form1"];
        }
        else 
        {
        theform = document.Form1;
        }
        
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTARGUMENT.value = eventArgument;
        theform.submit();
    }
    function _confirm(str)
    {
        window.document.all('btnApply').disabled = true;
        
        if (confirm(str))
        {
    //        document.Form1.submit();
            document.Form1.__doPostBack("btnApply","");
            return true;
     
        } 
        else
        {
            window.document.all('btnApply').disabled = false;
            return false;
        }
    } <INPUT style="Z-INDEX: 107; LEFT: 0px; WIDTH: 48px; POSITION: absolute; TOP: 512px; HEIGHT: 24px" type="hidden" value="Button" name="__EVENTTARGET"> 
    <INPUT style="Z-INDEX: 108; LEFT: 48px; WIDTH: 48px; POSITION: absolute; TOP: 512px; HEIGHT: 24px" type="hidden" value="Button" name="__EVENTARGUMENT">
    aspx.cs:btnApply.Attributes.Add("onclick", "return _confirm('" + strPrompt + "');");连page load也进不去了:(
      

  10.   

    document.Form1.__doPostBack("btnApply","");
    改为:
    __doPostBack("btnApply","");
      

  11.   

    <INPUT style="Z-INDEX: 107; LEFT: 0px; WIDTH: 48px; POSITION: absolute; TOP: 512px; HEIGHT: 24px" type="hidden" value="Button" name="__EVENTTARGET"> 
    <INPUT style="Z-INDEX: 108; LEFT: 48px; WIDTH: 48px; POSITION: absolute; TOP: 512px; HEIGHT: 24px" type="hidden" value="Button" name="__EVENTARGUMENT">改为:
    放在form中:
    <input type="hidden" name="__EVENTTARGET"> 
    <input type="hidden" name="__EVENTARGUMENT">
      

  12.   

    使用消息框
    使用警告、提示和确认
    可以使用警告、确认和提示消息框来获得用户的输入。这些消息框是 window 对象的接口方法。由于 window 对象位于对象层次的顶层,因此实际应用中不必使用这些消息框的全名(例如 "window.alert()"),不过采用全名是一个好注意,这样有助于您记住这些消息框属于哪个对象。警告消息框
    alert 方法有一个参数,即希望对用户显示的文本字符串。该字符串不是 HTML 格式。该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用户必须先关闭该消息框然后才能继续进行操作。 window.alert("欢迎!请按“确定”继续。");
    确认消息框
    使用确认消息框可向用户问一个“是-或-否”问题,并且用户可以选择单击“确定”按钮或者单击“取消”按钮。confirm 方法的返回值为 true 或 false。该消息框也是模式对话框:用户必须在响应该对话框(单击一个按钮)将其关闭后,才能进行下一步操作。 var truthBeTold = window.confirm("单击“确定”继续。单击“取消”停止。");
    if (truthBeTold) {
    window.alert("欢迎访问我们的 Web 页!");
    }  else  window.alert("再见啦!");
    提示消息框
    提示消息框提供了一个文本字段,用户可以在此字段输入一个答案来响应您的提示。该消息框有一个“确定”按钮和一个“取消”按钮。如果您提供了一个辅助字符串参数,则提示消息框将在文本字段显示该辅助字符串作为默认响应。否则,默认文本为 "<undefined>"。 与alert( ) 和 confirm( ) 方法类似,prompt 方法也将显示一个模式消息框。用户在继续操作之前必须先关闭该消息框 var theResponse = window.prompt("欢迎?","请在此输入您的姓名。");
      

  13.   

    谢谢 古道热肠 按您的改了以后,还是只可以进入page load,
    进不了我的
    private void btnApply_Click(object sender, System.EventArgs e)
    真是好奇怪~
      

  14.   

    1、确认:
    <form id="Form1" method="post" runat="server">
        <input type="hidden" name="__EVENTTARGET"> 
        <input type="hidden" name="__EVENTARGUMENT">
        ...
    </from>2、确认form的id是否和function __doPostBack(eventTarget, eventArgument) 中的一致:
    if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 
    {
        theform = document.forms["Form1是form的id"];
    }
    else 
    {
        theform = document.Form1是form的id;
    }
      

  15.   

    那两个INPUT原本就在form中的,
    <form id="Form1" method="post" runat="server">
    ...
    </form>
      

  16.   

    修改
    btnApply.Attributes.Add("onclick", "return _confirm('" + strPrompt + "');");

    btnApply.Attributes.Add("onclick", "_confirm('" + strPrompt + "');");修改_confirm(str)方法为:
    function _confirm(str)
    {
        window.document.all('btnApply').disabled = true;
        
        if (confirm(str))
        {
            __doPostBack("btnApply","");
        } 
        else
        {
            window.document.all('btnApply').disabled = false;
        }
    }
      

  17.   

    现在可以激活我的服务端的CLICK事件了,就是按取消也会如此,算了,我放弃了~ 谢谢名位的帮助,结贴~