• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

disable a ASP button in Javascript

Can anyone tell me how to disable a ASP button in Javascript?  I've tried all the following, but none seem to work.

var btn1 = document.getElementById("<%= btnSave.ClientID %>");
btn1.disabled = 1

OR
 
btn1.disabled = true

OR

btn1.disabled = "true"

OR  

btn1.disabled = "disabled"
0
HLRosenberger
Asked:
HLRosenberger
  • 9
  • 6
  • 5
  • +2
1 Solution
 
cmalakarCommented:
>> btn1.disabled = true

shd do the trick.
0
 
dj_alikCommented:
document.getElementById('<%= button.ClientID %>').disabled = true;
0
 
HLRosenbergerAuthor Commented:
I tried both of those.  NOthing seems to work.  And I almost certain I've done this before.  I must have a type somewhere, because if I but an alert right after the line of code that disables the button, it never gets there.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
cmalakarCommented:
Check this alert first..

alert("<%= btnSave.ClientID %>");

before document.getElementById(), and see if btnSave.ClientID is really giving the button name.

0
 
cmalakarCommented:
If that check passes, .. after that check..

alert(btn1); after the line document.getElementById().. and check if you get a not null value.
0
 
HLRosenbergerAuthor Commented:
that give me this name for the control:

ctl00_PageContent_btnSave

Why is this stuff pre-pended to the name?  
0
 
cmalakarCommented:
That is how it is coming I believe.
0
 
dj_alikCommented:
Can you explain your server event? When you did.
0
 
HLRosenbergerAuthor Commented:
If I do this:

var mybtn = document.getElementById("<%= btnSave.ClientID %>");
alert(mybtn)

the alert gives a null.  
0
 
cmalakarCommented:
Yes, because the button Id, you are referring inside document.getElementById() is not correct.
0
 
dj_alikCommented:
Set the code on page load
0
 
HLRosenbergerAuthor Commented:
So, what's the solution?  I want to disable the button.   I have other controls on the page, and I reference them to change color, focus, etc., all in JavaScript and all work fine.  And I'm using document.getElementById() for all.  

What could be different about the button?
0
 
cmalakarCommented:
An ASP expert can give you the solution, for why btnSave.ClientID is giving the above value you said.

Otherway to solve this problem is

Check the button Id from the html source, and use that id instead of "<%= btnSave.ClientID %>"

For ex, if your button id is "myButton" then

var btn1 = document.getElementById("myButton");
0
 
dj_alikCommented:
What type of Button you are using?
0
 
HLRosenbergerAuthor Commented:
Here's the ASP for the button:

<TdsIncidents:ThemeButton runat="server" id="btnSave" button-causesvalidation="True" button-commandname="UpdateData" button-onclientclick="return Save_onclick()" button-text="&lt;%# GetResourceValue(&quot;Btn:Save&quot;, &quot;TdsIncidents&quot;) %>" postback="True"></TdsIncidents:ThemeButton>


is this a standard ASP button?
0
 
dj_alikCommented:
If you try to add in will work?
 button-onclientclick="
this.disabled = true;
return Save_onclick()"

0
 
HLRosenbergerAuthor Commented:
dj_alik:

if I do this:

button-onclientclick="
this.disabled = true;
return Save_onclick()"

will it disabled upon click?  I do not want that, as I am first doing validation.  

I want to disable it after the  validation.
0
 
HLRosenbergerAuthor Commented:
a
0
 
HLRosenbergerAuthor Commented:
Here is what I am seeing, and I do not understand what is going on:

I am successfully referencing other controls on my page like this:

var category = document.getElementById("<%= lstCategory.ClientID %>");

However, this does not work for my SAVE button.  

var btn= document.getElementById("<%= btnSave.ClientID %>");
alert(btn)

Alert displays "null".

So, I displayed the source after the page is rendered.  And this is the ID for the SAVE button:  ctl00_PageContent_btnSave__Button

So, I did this:

var btn= document.getElementById("<%= ctl00_PageContent_btnSave__Button.ClientID %>");

But this fails at runtime with the following error:

'ctl00_PageContent_btnSave__Button' is not declared.

So, if the declared ID is different than the generated ID, how does one reference this control via Javascript??

Thanks.









 
0
 
leakim971PluritechnicianCommented:
// in your last test, remove <%= .ClientID %>
var btn= document.getElementById("ctl00_PageContent_btnSave__Button");

Open in new window

0
 
Paul JacksonCommented:
In your page_load in your code-behind add this code :
Dim scripts As String = "<script language='JavaScript' type='text/javascript'>"
            scripts += "function getBtnSave() {return '" & btnSave.ClientID & "';} "
            scripts += "</script>"
            If (Not Me.ClientScript.IsStartupScriptRegistered("getBtnSave")) Then
                Me.ClientScript.RegisterStartupScript(Me.GetType, "getBtnSave", scripts)
            End If

Open in new window

Then in your javascript :
document.getElementById(getBtnSave()).disabled = false;

Open in new window

0
 
HLRosenbergerAuthor Commented:
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now