【问题标题】:Remove attribute from code behind in asp.net从 asp.net 中的代码中删除属性
【发布时间】:2020-08-30 05:19:31
【问题描述】:

我有一个网格,想根据后面代码中的条件添加或删除禁用属性。

下面是jquery代码:

  $("#myGrid tbody :input").attr("disabled", "disabled");

我想从后面的代码中做同样的事情。我在#myGrid 元素上提到了“runat=server”。在后面的代码中,我尝试了如下代码:

if (condition-here)
{
  myGrid .Attributes.Remove("disabled");
}
else
{
  myGrid.Attributes.Add("disabled","disabled");
}

【问题讨论】:

    标签: c# jquery asp.net webforms


    【解决方案1】:

    您正在尝试禁用/启用网格中的所有输入。如果您使用 AJAX 并需要它用于更新面板,我们可以使用 ScriptManager 来做到这一点,那么:

    所以,基本上你的代码是这样的:

     if (condition-here)
       { 
          //remove attribute
        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyScript", "$('#myGrid tbody :input').removeAttr('disabled');", true);
       }
     else
       {
       //add attribute
        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyScript", "$('#myGrid tbody :input').attr('disabled', 'disabled');", true);
       }
    

    如果你不使用 AJAX,那么使用这个:

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "$('#myGrid tbody :input').attr('disabled', 'disabled');", true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多