Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't access my web control in javascript

Status
Not open for further replies.

gib99

Programmer
Mar 23, 2012
51
CA
Hello,

I'm sorry if this has been answered before, but I did try searching and found no solutions to this:

I'm working with a web application that needs to get access to an ASP.NET web control (a button) in javascript. The problen is that since the control is run on the server, javascript can't access it in the standard way (i.e. document.getElementById(controlId);).

I've actually solved this problem before in a different application, but my solution there doesn't seem to work here for some reason.

I have this in an aspx file:

Code:
<script...>

...

function myfun()
{
var b = document.getElementById("<%=SaveButton.ClientID%>");
alert(b);
}

</script>

...

<asp:Button ID="SaveButton" Text="save" OnClientClick="myfun()" ClientIDMode="Static" runat="server" UseSubmitBehavior="False" />

I have a designer class in which the button is declared (and therefore exists in the server-side codebehind):

Code:
protected global::System.Web.UI.WebControls.Button SaveButton;

But when I click on my button, the alert box says "null".

Why am I not able to get my button in the javascript function?

Some things to note:

*The button exists within a content tag:

Code:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

*I'm working within Visual Studios 2008.
 
Why do you have that declaration for the button? Was this page originally created in 1.0 or 1.1 and converted to 3.5?
There is no reason for that code anymore. I'm not sure if that is the problem, but it could be.
 

Another variation to try:
Code:
<asp:Button ID="SaveButton" Text="save" OnClientClick="myfun([red]this[/red])" ClientIDMode="Static" runat="server" UseSubmitBehavior="False" />

and JS
Code:
function myfun(btn) {
            alert(btn.id);
        }


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
jbenson001 said:
Why do you have that declaration for the button? Was this page originally created in 1.0 or 1.1 and converted to 3.5?

Probably, I didn't write the code, I'm only debuggin it.

MarkSweetland said:
Another variation to try

Yes, that worked! Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top