在客户端禁用 ASP 按钮后,无法从代码隐藏中启用它自己

Can't get an ASP Button to enable itself from code-behind after disabling it client-side

我是 ASP.NET 的新手,所以这肯定是因为我缺乏理解。

我遇到了一个 ASP 按钮,它同时设置了 OnClientClickOnClick

<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="Server">
    <asp:UpdatePanel ID="upSendData" runat="server" UpdateMode="Conditional" RenderMode="Block">
        ...
    </asp:UpdatePanel>
    <asp:Button ID="btnSave" runat="server" 
                OnClientClick="if(!CanSave()){return false;}; DisableSaveButton();"
                OnClick="btnSave_Click" 
                Text="<%$ Resources: btnSave %>"
                CssClass="cmdFormButton"
                UseSubmitBehavior="false" 
                Width="220px" />
</asp:Content>

OnClientClick 调用一个禁用按钮的 JS 函数,以避免用户不小心多次点击它:

function DisableSaveButton() {
    document.getElementById("<%=btnSave.ClientID %>").disabled = true;
}

然后代码隐藏 OnClick 执行保存过程...但是如果出现问题,我想启用返回按钮。所以我这样做了:

protected void btnSave_Click(object sender, EventArgs e)
{
    if (SaveData())
    {
        // Redirects to another page
    }
    else
    {
        btnSave.Enabled = true;  // This is "true" already
    }
}

但是没用。客户端代码在代码隐藏之前被正确执行,按钮被禁用。但是如果我调试代码隐藏,btnSave.Enabled returns true 已经,它不会重新启用它。

我知道 ASP Enabled 属性 和 HTML disabled 属性不一样,但我有点期待 ASP知道如何处理这个。

此外,我不确定是否(或如何)可以从代码隐藏访问 disabled 属性。

有什么想法吗?建议?提前致谢!

您可以修改属性...

 else
    {
        btnSave.Enabled = true;  // This is "true" already
        btnSave.Attributes.Remove("disabled");
    }

此外,您需要将 UseSubmitBehavior="false" 应用到要调用的``后面的代码 的按钮

<asp:Button ID="btnSave" UseSubmitBehavior="false"  
            runat="server" Text="Button" OnClick="btnSave_Click" 
            OnClientClick="DisableSaveButton();" />

我已经有几年没有使用 WebForms 了,所以这是一个有根据的猜测

我们终于设法解决了这个问题。这就像 "easy" 注册一个脚本,该脚本调用 JS 函数以从代码隐藏在客户端启用按钮。

CS:

protected void btnSave_Click(object sender, EventArgs e)
{
    if (SaveData())
    {
        // Redirects to another page
    }
    else
    {
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
            "showSaveButtons_" + this.ClientID, "EnableSaveButton();", true);
    }
}

ASPX:

function EnableSaveButton() {
    document.getElementById("<%=btnSave.ClientID %>").disabled = false;
}

我还希望能够通过更新面板中的代码隐藏来控制按钮禁用状态。我使用一个函数来注入客户端 onclick 处理程序属性,该属性将设置禁用属性以防止多次单击并将 value 属性 更改为 "Saving"。然后在我的 OnClick 代码中,如果我设置 Enabled=false 并强制我的 UpdatePanel 更新 upSendData.Update(),这将强制面板重绘处于当前状态的所有控件。 btn1 被绘制为启用,而 btn2 被禁用。值(文本)也自动重置。

ASPX 页面

<asp:UpdatePanel ID="upSendData" UpdateMode="Conditional">
<ContentTemplate>
....
    <asp:Button ID="btn1" runat="server" Text="Email Instructions" OnClick="btn1_Click" Enabled="false" />        
    <asp:Button ID="btn2" runat="server" Text="Save" OnClick="btn2_Click" />
</ContentTemplate>
</asp:UpdatePanel>

ASPX.CS

public void disableSubmitButtonOnClick(Button objButton, String message = "Processing") {
        objButton.CausesValidation = false;
        string validationGroup = objButton.ValidationGroup;
        if (string.IsNullOrEmpty(validationGroup))
            objButton.Attributes.Add("onclick", String.Format("if (Page_ClientValidate()) {{this.value='{0}...';this.disabled=true;{1} }}", message, objButton.Page.ClientScript.GetPostBackEventReference(objButton, "").ToString()));
        else
            objButton.Attributes.Add("onclick", String.Format("if (Page_ClientValidate(\"{2}\")) {{this.value='{0}...';this.disabled=true; {1} }}", message, objButton.Page.ClientScript.GetPostBackEventReference(objButton, "").ToString(), validationGroup));
}
Page_Load()
{
    if(!IsPostBack())
    {
        disableSubmitButtonOnClick(btn1);
        disableSubmitButtonOnClick(btn2);
    }
}

protected void btn2_Click()
{
btn2.Enabled = false;
btn1.Enabled = true;
upSendData.Update(); 
}

如果按钮在UpdatePanel之外,则需要RegisterClientScript重置按钮状态(假设jQuery):

public void resetSubmitButton(Button objButton, Boolean state=true)
{
    String sState = (!state).ToString().ToLower();
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "reset" + objButton.ID, String.Format("$('#{0}').val('{1}');$('#{0}').attr('disabled', {2});", objButton.ClientID, objButton.Text, sState), true);
}

protected void btn_Click(object sender, EventArgs e)
{
    resetSubmitButton((Button)sender);
}