disable and enable controls in HTML from code-behind

davism used Ask the Experts™

I have a html buttons that I need to keep at html buttons in order to keep a postback from happening. I can't enable the html with a runat="server" because that defeats the purpose of having the actions be on the client-side.

However, I want to disable those buttons from the code-behind written in C# when certain conditions are present and I want to enable under certain conditions.

How can I achieve that from the code-behind?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


Let me further clarify this a little. I have an updownspinner that I have. The increment and decrement are working fine and it does not impose a post-back situation when I click either of those. The reason, the html button does not by default use a postback like the asp:Button does. So, I can increment and decrement the values on the with the client side.

Now, for those buttons, I only want them to be enabled when information is populated on the text box that the value is incrementing or decrementing by use of the buttons.

I can disable the buttons on the html control but that will not help because I would need to be able to enable them when the field is populated.

Is there a way I can enable those button fields from the code-behind?

Any assistance on this would be greatly appreciated.


you can give your html controls an ID and runat="server" property and you change the settings from your code-behind

Example :
<img id="myImg" runat=server" />


True and I have done that BUT when that happens it uses a postback. That is something I do not want to do. I am only using the server to control the visibility of them. Everything else I want to do on the client.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

i'm sorry .. then you should use an JavaScript Code for that

check this example



Right, but that doesn't get to the crux of the issue.

What I need to do is from the code-behind, how do I call like the:


of the

function Disab(val)

So, the function is in the client-side as noted with the function Disab(val) but I need the
sent via the code behind page.

How can I do that?

you can't do it from your code behind without a calling post back
but you can hide the page refresh from your user by put all your controls in an

but you can't do somthing from code behind without refresh the page and call post back method


I would only need to do that under certain events.

For instance...

Default would be that they are not visible.

Certain activity transpires...

The textbox is populated.

At that point, I want to enable the boxes ((or make them visible) whichever). The client side would be utilized at that point for the boxes.

Then when another button is pressed like a "Save" button, I want to disable the boxes ((or make them invisible) whichever).

Is that possible?
Rufus WilliamSenior Software Architect

can try this solution...

Create a protected string variable in the Code behind side..
eg. protected string strDisabled
when ever u want to disable the box set its value to "Disabled"

then in the html side u can use the below line..
<button id="button1" <%=strDisabled %> >......</button>

it should work...


I'm going to try a tact whereby I'm doing to invoke the javascript function with adding an attribute to an existing control.

Basically, tempting to run a javascript function from a code-behind using the control. I will see what I can achieve with that.

If that doesn't work I will tempt what you mentioned arufus.  (Thinking more about what you mentioned it sounds like that might be a good solution as well.) But let me try both and I will let you all know asap.


To disable button from code behind use below code
Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('Button1').disabled = true;</script>");

Open in new window



Nope that did not work. I had the strDisabled value (as protected) defaulting to "disabled='true'" and when the certain condition exists it changed it to strDisabled="disabled='false'"

rajapandian_81, correct me if I am wrong but not the values have to be enclosed in quotes?

I tried:

Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = '" + value + "';</script>");

Open in new window


rajapandian_81...ooops my apology...hit the submit button too soon.

However, the code did not work. It did not change it back to enabled.

Any other thoughts on this?

To disable, simply use below code
Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = true;</script>");

To Enable, use below code
Page.RegisterStartupScript("Enable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = false;</script>");

Let me know if you have problem on this.


Understand and that worked but how can I put it on one line and make the true or false variable?

I tried the attached but it doensn't work. How can I get it to lowercase as well?

Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = " + Convert.ToString(value)  + ";document.getElementById('btnUpDownSpinDown').disabled = " + Convert.ToString(value) +  ";</script>");

Open in new window

Rufus WilliamSenior Software Architect

davism, strDisabled should be empty when u want to enable the control and strDisabled should be "Disabled" when u want to disable the control...

FYI, "disabled=true / false" workz only for ASP.NET controls

To Enable or Disable two controls using a variable use below code.
If strStatus is "true", control will be disabled.
If strStatus is "false", control will be enabled.
string strStatus = "true";
Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = " + strStatus + "; document.getElementById('btnUpDownSpinDown').disabled = " + strStatus + ";</script>");

Open in new window


Right, but strStatus or "value" in my example is a boolean value.

However, I get an compilation error when I try

Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = " + Convert.ToString(value).ToLower  + ";document.getElementById('btnUpDownSpinDown').disabled = " + Convert.ToString(value).ToLower  +  ";</script>");

I'm confused on that aspect as why would I get that?


Ok, stupid mistake. I needed "()"'s on the .ToLower.

Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('btnUpDownSpinUp').disabled = " + Convert.ToString(value).ToLower()  + ";document.getElementById('btnUpDownSpinDown').disabled = " + Convert.ToString(value).ToLower()  +  ";</script>");

Everything is working out great now and as expected.

Thanks much for the assistance on that and greatly appreciated.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial