[SOLVED] Remove attribute from code behind in asp.net

Issue

I have a grid and want to add or remove the disable attribute based on a condition from the code behind.

Below is the jQuery code:

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

I want to do the exact same thing from code behind. I have mentioned "runat=server" on #myGrid element. In the code behind, I tried something like below:

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

Solution

You are trying to disable/enable all inputs in the grid. We can do this by using ScriptManager if you using AJAX and need it for update panel, then:

So, basically you code be like:

 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);
   }

If you are not using AJAX, then use this:

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

Answered By – Rishu Ranjan

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *