代码之家  ›  专栏  ›  技术社区  ›  Simple Code

非可开票成员“System.Web.UI.Page.ClientScript”不能像方法一样使用

  •  0
  • Simple Code  · 技术社区  · 7 年前


    我试图冻结一个按钮时,点击,这样用户就不会双击意外造成重复实体。这是我的密码:

          private void FreezeButton()
        {
            var sb = new StringBuilder();
            sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
    
            sb.Append("var oldPage_IsValid = Page_IsValid; var oldPage_BlockSubmit = Page_BlockSubmit;");
            sb.Append("if (Page_ClientValidate('" + btnAdd.ValidationGroup + "') == false) {");
            sb.Append(" Page_IsValid = oldPage_IsValid; Page_BlockSubmit = oldPage_BlockSubmit; return false; }} ");
    
            sb.Append("this.value = 'Processing...';");
            sb.Append("this.disabled = true;");
    
            sb.Append(Page.ClientScript(btnAdd, null) + ";");
            sb.Append("return true;");
    
            string submitButton = sb.ToString();
    
            btnAdd.Attributes.Add("onclick", submitButton);
        }
    

    首先,我尝试在没有 页面.ClientScript 它给了我一个错误:名字 'ClientScript' 在当前上下文中不存在

    然后我看着 This

    2 回复  |  直到 7 年前
        1
  •  1
  •   Tetsuya Yamamoto    7 年前

    Page.ClientScript 是一个属性,不能像方法一样使用它。可能你在找 Page.ClientScript.RegisterClientScriptBlock()

    假设你在处理 btnAdd FreezeButton 方法事件处理时,应该替换要包含的内容 RegisterClientScriptBlock

    protected void FreezeButton(object sender, EventArgs e)
    {
        var sb = new StringBuilder();
        sb.Append("function validate() { ")
    
        // script content here, skipped for brevity
    
        sb.Append("}");
    
        // use RegisterClientScriptBlock to attach script content into <script> tag inside page body
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Example", sb.ToString(), true);
    
        // handle client-side event click if the button is a server control
        btnAdd.OnClientClick = "validate()";
    }
    

    然后,处理的服务器端click事件 这样地:

    <asp:Button ID="btnAdd" runat="server" OnClick="FreezeButton" ... />
    
        2
  •  0
  •   Simple Code    7 年前

    我找到了解决办法:

      private void FreezeButton()
        {
            var sb = new StringBuilder();
            sb.Append("if (typeof(Page_ClientValidate) == 'function') { ");
    
            sb.Append("var oldPage_IsValid = Page_IsValid; var oldPage_BlockSubmit = Page_BlockSubmit;");
            sb.Append("if (Page_ClientValidate('" + btnAdd.ValidationGroup + "') == false) {");
            sb.Append(" Page_IsValid = oldPage_IsValid; Page_BlockSubmit = oldPage_BlockSubmit; return false; }} ");
    
            sb.Append("this.value = 'Processing...';");
            sb.Append("this.disabled = true;");
    
            sb.Append(Page.ClientScript.GetPostBackEventReference(btnAdd, null) + ";");
            sb.Append("return true;");
    
            string submitButtonOnclickJs = sb.ToString();
    
            btnAddReceipt.Attributes.Add("onclick", submitButtonOnclickJs);
        }
    
    推荐文章