Javascript Submit Problem

C

czuvich

All,

I seem to be having an issue with calling the
'document.getElementById()' in my javascript function. I have a
checkbox where runat=server is set since I am using it in the code
behind (this is asp.net 2.0). I then have a submit button. I want to
check to see if this checkbox is checked in the javascript button. But
since the control is runat=server, whenever I call the
getElementById(checkbox) it returns a null value (can't recognize the
control).

Of course whenever I remove the runat=server it runs fine, but I need
this to be able to use in my code behind. I was wanting to know if
there is a way to reference a server object in javascript. If so.. how?
Thanks.
 
L

Laurent Bugnion

Hi,
All,

I seem to be having an issue with calling the
'document.getElementById()' in my javascript function. I have a
checkbox where runat=server is set since I am using it in the code
behind (this is asp.net 2.0). I then have a submit button. I want to
check to see if this checkbox is checked in the javascript button. But
since the control is runat=server, whenever I call the
getElementById(checkbox) it returns a null value (can't recognize the
control).

Of course whenever I remove the runat=server it runs fine, but I need
this to be able to use in my code behind. I was wanting to know if
there is a way to reference a server object in javascript. If so.. how?
Thanks.

What HTML code gets produced? What ID do you use in your getElementById?
Often, people confuse the control's ID and the ClientID. The ID is the
one you give to the control. However, this ID gets modified by ASP.NET,
because it must ensure that the ID is unique throughout the page. This
is why your server-side code must pass the ClientID to the client-side
code, using ClientScriptManager.RegisterClientScriptBlock

HTH,
Laurent
 
C

czuvich

Laurent said:
Hi,


What HTML code gets produced? What ID do you use in your getElementById?
Often, people confuse the control's ID and the ClientID. The ID is the
one you give to the control. However, this ID gets modified by ASP.NET,
because it must ensure that the ID is unique throughout the page. This
is why your server-side code must pass the ClientID to the client-side
code, using ClientScriptManager.RegisterClientScriptBlock

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Hey,
Thanks for the reply. How interesting is that.... The ID it gave it in
the html was ctl00_ContentPlaceHolder1_ckbUseDate. I'll do some more
research into the RegisterClientScriptBlock. Right now, I have a .js
file. Will this ID change each time the page is run.. or will that name
be constant? Thanks!
 
L

Laurent Bugnion

Hi,
Hey,
Thanks for the reply. How interesting is that.... The ID it gave it in
the html was ctl00_ContentPlaceHolder1_ckbUseDate. I'll do some more
research into the RegisterClientScriptBlock. Right now, I have a .js
file. Will this ID change each time the page is run.. or will that name
be constant? Thanks!

To be honest, not very interesting ;-) It's what I told you. You give an
ID to your control on the ASPX page, but if your control runs inside
another control (user control), the framework creates a unique ID named
ClientID. In your code behind, you must get the ClientID and pass it to
JavaScript

Example with a Label in a Control:

string strScript = "<script type='text/javascript'>"
+ Environment.NewLine
+ "var strLabelClientId = " + lblTest.ClientID + ";"
+ Environment.NewLine
+ "</script>";

this.Page.ClientManager.RegisterClientScriptBlock( typeof( MyControl ),
"myScript", strScript );

HTH,
Laurent
 
C

czuvich

Thanks a lot! I got it working.

Laurent said:
Hi,


To be honest, not very interesting ;-) It's what I told you. You give an
ID to your control on the ASPX page, but if your control runs inside
another control (user control), the framework creates a unique ID named
ClientID. In your code behind, you must get the ClientID and pass it to
JavaScript

Example with a Label in a Control:

string strScript = "<script type='text/javascript'>"
+ Environment.NewLine
+ "var strLabelClientId = " + lblTest.ClientID + ";"
+ Environment.NewLine
+ "</script>";

this.Page.ClientManager.RegisterClientScriptBlock( typeof( MyControl ),
"myScript", strScript );

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top