在回发之前禁用ascx页面中的按钮

时间:2011-03-02 14:04:00

标签: asp.net

如何禁用ascx页面中的按钮?我知道如何在aspx页面上执行以下操作:

btnSave.Attributes.Add("onclick", 
  "this.disabled=true;this.value='Please Wait...';needToConfirm=false;" +
  ClientScript.GetPostBackEventReference(btnSave, "").ToString());

但是ascx页面中没有ClientScript函数。

5 个答案:

答案 0 :(得分:3)

您不需要代码中的ClientScript,您也可以直接在按钮标记中将其作为OnClientClick属性:

<asp:Button id="btnSave" runat="server" OnClientClick="this.disabled=true; this.value='Please Wait...'; needToConfirm=false;" Text="Save" />

答案 1 :(得分:1)

您可以使用用户控件(ascx)中的Page.ClientScript访问ClientScript属性。

答案 2 :(得分:1)

您可以通过控件的Page属性访问ClientScript

Page.ClientScript.GetPostBackEventReference(btnSave, "")

答案 3 :(得分:1)

您是否尝试过:Page.ClientScript.GetPostBackEventReference?

答案 4 :(得分:0)

以下是我解决问题的方法。在每一页中,我都有三个div:

<div align="center" id="divWait" style="display:none"><asp:Label ID="lblSaveorCancel" runat="server"></asp:Label></div>

<div style="display:block" id="divMain">

----  page actual content here  ------

</divMain>

<div id="divBut" style="text-align:center;display:block">
<asp:button id="SaveBtn" runat="server" CssClass="button" Text="Save" OnClientClick="return Validate('save');"/>
<asp:button id="CancelBtn" runat="server" CssClass="button" Text="Cancel" OnClientClick="return ShowWaitDiv('cancel');"/>
</div>

然后我添加了脚本:

function Validate(saveorcancel) {

-----  validation checks for data on the page  ------

}

function ShowWaitDiv(saveorcancel) {
    var div = document.getElementById(divWait.id);
    div.style.display = "block";
    var div1 = document.getElementById(divMain.id);
    div1.style.display = "none";
    var div2 = document.getElementById(divBut.id);
    div2.style.display = "none";
    if (saveorcancel == 'save') {
        document.getElementById('<%= lblSaveorCancel.ClientID %>').innerHTML = 'Saving data, please wait...';
    }
    else {
        document.getElementById('<%= lblSaveorCancel.ClientID %>').innerHTML = 'Redirecting, please wait...';
    }
    return true;
}

简单,快捷,用户可以立即看到点击按钮的结果,无法再次点击任何按钮。

相关问题