Marc Davis
asked on
disable and enable controls in HTML from code-behind
All,
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?
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?
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" />
Example :
<img id="myImg" runat=server" />
ASKER
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.
i'm sorry .. then you should use an JavaScript Code for that
check this example
http://www.houseofscripts.com/scripts/javascripts/disablebm.htm
check this example
http://www.houseofscripts.com/scripts/javascripts/disablebm.htm
ASKER
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:
if(val=="buttn1")
{frm.Button1.disabled=true ;frm.Butto n2.disable d=false}
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?
What I need to do is from the code-behind, how do I call like the:
if(val=="buttn1")
{frm.Button1.disabled=true
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
<asp:UpdatePanel>
but you can't do somthing from code behind without refresh the page and call post back method
but you can hide the page refresh from your user by put all your controls in an
<asp:UpdatePanel>
but you can't do somthing from code behind without refresh the page and call post back method
ASKER
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?
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?
Davism,
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...
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...
ASKER
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.
Thanks!
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.
Thanks!
Hi,
To disable button from code behind use below code
To disable button from code behind use below code
Page.RegisterStartupScript("Disable", "<script language=JavaScript>document.getElementById('Button1').disabled = true;</script>");
ASKER
arufus,
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='fal se'"
rajapandian_81, correct me if I am wrong but not the values have to be enclosed in quotes?
I tried:
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='fal
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>");
ASKER
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?
However, the code did not work. It did not change it back to enabled.
Any other thoughts on this?
Hi,
To disable, simply use below code
Page.RegisterStartupScript ("Disable" , "<script language=JavaScript>docume nt.getElem entById('b tnUpDownSp inUp').dis abled = true;</script>");
To Enable, use below code
Page.RegisterStartupScript ("Enable", "<script language=JavaScript>docume nt.getElem entById('b tnUpDownSp inUp').dis abled = false;</script>");
Let me know if you have problem on this.
To disable, simply use below code
Page.RegisterStartupScript
To Enable, use below code
Page.RegisterStartupScript
Let me know if you have problem on this.
ASKER
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?
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>");
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
FYI, "disabled=true / false" workz only for ASP.NET controls
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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>docume nt.getElem entById('b tnUpDownSp inUp').dis abled = " + Convert.ToString(value).To Lower + ";document.getElementById( 'btnUpDown SpinDown') .disabled = " + Convert.ToString(value).To Lower + ";</script>");
I'm confused on that aspect as why would I get that?
However, I get an compilation error when I try
Page.RegisterStartupScript
I'm confused on that aspect as why would I get that?
ASKER
Ok, stupid mistake. I needed "()"'s on the .ToLower.
Page.RegisterStartupScript ("Disable" , "<script language=JavaScript>docume nt.getElem entById('b tnUpDownSp inUp').dis abled = " + Convert.ToString(value).To Lower() + ";document.getElementById( 'btnUpDown SpinDown') .disabled = " + Convert.ToString(value).To Lower() + ";</script>");
Everything is working out great now and as expected.
Thanks much for the assistance on that and greatly appreciated.
Page.RegisterStartupScript
Everything is working out great now and as expected.
Thanks much for the assistance on that and greatly appreciated.
ASKER
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.
Thanks