且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免使用脚本在asp.net中的formview中单击多个按钮

更新时间:2023-09-29 18:16:40

如果使用更新面板,一旦客户端使用css单击按钮,您可以使屏幕变灰。这样可以防止二次点击。



 <   ProgressTemplate  >  
< div class = divWaiting >





。divWaiting {
position:absolute;
background-color:black;
z-index:2147483647!important;
不透明度:0.6;
溢出:隐藏;
text-align:center;顶部:0;左:0;
身高:100%;
宽度:100%;
填充顶部:20%;
}


我成功地使用了以下解决方案。我从我的同事那里得到了这个剧本。它使用JavaScript函数来禁用和启用按钮。禁用它时也会将按钮图像设置为.gif以指示处理。



如果已经将其作为另一个线程的解决方案发布,请原谅我。



  function  disableBtn(btnID,newText){
// 初始化以避免'Page_IsValid未定义'JavaScript错误
Page_IsValid = 空跨度>;
// 检查页面是否请求任何验证
// 如果是,请检查页面是否有效
if typeof (Page_ClientValidate)== ' function '){
Page_ClientValidate();
// 您也可以传递验证组名称
}
// 变量
var btn = document .getElementById(btnID);
var isValidationOk = Page_IsValid;
/ * ******* NEW UPDATE *********** ************************* /
// 如果不是IE,则在重定向/渲染之前启用卸载按钮
if navigator .appName!== ' Microsoft Internet Explorer'
{
EnableOnUnload(btnID,btn.value);
}
/ * ********** END UPDATE *** ************************* /
// isValidationOk不为空
如果(isValidationOk!== null ){
// 页面有效
if (isValidationOk){
btn.disabled = true ;
btn.value = newText;
btn.style.background = ' url(〜/ images / ajax-loader.gif)'跨度>;
}
其他 { // 页面无效
btn.disabled = false ;
}
}
其他 { // 页面没有任何验证请求
setTimeout( setImage(' + btnID + ') 10 跨度>);
btn.disabled = true ;
btn.value = newText;
}
}

// 设置背景图片按钮
function setImage(btnID)
{
var btn = document .getElementById(btnID);
btn.style.background = ' url(images / loading.gif)';
}

// 启用按钮并恢复原始文本值
function EnableOnUnload(btnID,btnText)
{
window .onunload = function ()
{
var btn = 文档跨度> .getElementById(btnID);
btn.disabled = false ;
btn.value = btnText;
}
}





在UseSubmitBehaviour设置为false的情况下,包含对disableBtn OnClientClick的调用。



 <   asp:button     id   =  btnSubmit    runat   =  server    onclientclick   =  disableBtn(this.id,'正在提交...')    text   = 提交    usesubmitbehavior pan>  =  False    xmlns:asp   = #unknown    /  >  


hi to all,
Here i need how to avoid multiple click, here i done that process.now need is if i click save validation message show two time.how to avoid that.

coding is:-

script:

function ClientSideClick(myButton) {

            if (typeof (Page_ClientValidate) == 'function')
               {
                if (Page_ClientValidate() == false)
                { return false; }
            }


            if (myButton.getAttribute('type') == 'button') {

                myButton.disabled = true;

            }



button is:-

<asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                                                                  CssClass="savebutton1231" EnableTheming="false" SkinID="skinBtnSave" onclientclick="ClientSideClick(this)" UseSubmitBehavior="false" OnClick="InsertButton_Click"></asp:Button>





i need how to avoid same validation message twice.

if using an updatepanel you can grey out the screen once the client clicks the button using css. this will prevent a secondary click.

<ProgressTemplate>
<div class="divWaiting">



.divWaiting{
position: absolute;
background-color: black;
z-index: 2147483647 !important;
opacity: 0.6;
overflow: hidden;
text-align: center; top: 0; left: 0;
height: 100%;
width: 100%;
padding-top:20%;
}


I have used the solution below with success. I had gotten this script from a co-worker of mine. It uses JavaScript functions to disable and enable the button. While disable it also sets the button image to a .gif to indicate processing.

Forgive me if this was already posted as a solution on another thread.

function disableBtn(btnID, newText) {
    //initialize to avoid 'Page_IsValid is undefined' JavaScript error
    Page_IsValid = null;
    //check if the page request any validation   
    // if yes, check if the page was valid
    if (typeof (Page_ClientValidate) == 'function') {
        Page_ClientValidate();
        //you can pass in the validation group name also
    }
    //variables
    var btn = document.getElementById(btnID);
    var isValidationOk = Page_IsValid;
    /********NEW UPDATE************************************/   
    //if not IE then enable the button on unload before redirecting/ rendering   
    if (navigator.appName !== 'Microsoft Internet Explorer')
    {
           EnableOnUnload(btnID, btn.value);   
    }   
    /***********END UPDATE ****************************/   
    // isValidationOk is not null
    if (isValidationOk !== null) {
        //page was valid
        if (isValidationOk) {
            btn.disabled = true;
            btn.value = newText;
            btn.style.background = 'url(~/images/ajax-loader.gif)';
        }
        else {//page was not valid
            btn.disabled = false;
        }
    }
    else {//the page don't have any validation request
        setTimeout("setImage('"+btnID+"')", 10);
        btn.disabled = true;
        btn.value = newText;
    }
}

//set the background image of the button
function setImage(btnID) 
{
    var btn = document.getElementById(btnID);
    btn.style.background = 'url(images/loading.gif)';
}

//enable the button and restore the original text value
function EnableOnUnload(btnID, btnText)
{
    window.onunload = function()
    {
            var btn = document.getElementById(btnID);
            btn.disabled = false;
            btn.value = btnText;
    }
}



Include a call to disableBtn OnClientClick with UseSubmitBehaviour set to false.

<asp:button id="btnSubmit" runat="server" onclientclick="disableBtn(this.id, 'Submitting...')" text="Submit" usesubmitbehavior="False" xmlns:asp="#unknown" />