'Remove attribute from code behind in asp.net
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 1:[1]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rishu Ranjan |
