This is a migrated thread and some comments may be shown as answers.

javascript and .NET

10 Answers 207 Views
Getting started with ASP.NET
This is a migrated thread and some comments may be shown as answers.
Maus
Top achievements
Rank 1
Maus asked on 19 Oct 2006, 02:11 PM
I've been searching around on the web for this for quite a while now, and thus far have come up empty-handed.  I have a page that has a checkbox and a dropdown list, among other things.  The idea is to disable the dropdown if the checkbox is checked, as it more or less makes the dropdown selection irrelevant.  And, this needs to be done without a postback.  The page uses a master-page, which contains the <head> tags, and thus any javascript functions.  I have writtin this function in there:
function toggle_list()    {  
    var chk = document.forms[0].chkExpire.checked //document.getElementById("chkExpire")  
    var ddl = document.getElementById("ddlLength")  
    if (chk)  
        { ddl.disabled = true; }  
    else 
        { ddl.disabled = false; }  
    //return  
    } 

and tie it in through the pageload in vb thus:
If Not IsPostBack Then 
'Code here edited out.  
chkExpire.Attributes.Add("onClick""javascript: toggle_list();")  
End If 

I think the javascript function is calling, but the object references seem to be failing.  I'm as yet unaware of any way to actually debug my javascript, so I'm groping more or less blindly.

Anyone have any ideas?

10 Answers, 1 is accepted

Sort by
0
Jason Maronge
Top achievements
Rank 1
answered on 19 Oct 2006, 09:44 PM
Try this javascript out:

<script type="text/javascript" >  
   window.toggle_list = function()    {     
     var chk = document.getElementById("<%=chkExpire.ClientID%>").checked;  
     var ddl = document.getElementById("<%=ddlLength.ClientID%>");  
   
     if (chk)     
         { ddl.disabled = true; }     
     else    
         { ddl.disabled = false; }     
     //return     
     }    
</script>  
    

Don't forget to use the ClientID of the controls when refering to them in javascript. 

Good luck...

Jason
0
Maus
Top achievements
Rank 1
answered on 20 Oct 2006, 01:28 PM
Ok, a couple of questions.  Do I leave the vb code that ties it in to the checkbox alone?  And what exactly does ClientID do?

I should probably note that while I have been trained in VB and C# on .NET, I've been more or less thrown into javascript unprepared and have been trying to figure it out as I go.

Edit:  One other thing.  The javascript resides on the masterpage, but the controls referenced are on an ordinary page, so it currently has no idea what chkExpire and ddlLength refer to.  Is there any way around this?
0
Jason Maronge
Top achievements
Rank 1
answered on 20 Oct 2006, 02:02 PM
You can leave the vb code the same.  Here is a link to MS that will explain what the ClientID is.  In short it is the ID of the control that is outputted to the client.  Say you have a checkbox (chk) control inside a panel (pnl).  The ClientID of the checkbox would be pnl_chk.  You can not just do a document.getElementById("chk"), it will return a null.  You have to make sure that you are using the ClientID (complete rendered ID).  document.getElementById("pnl_chk").  But since you may not know it you can use the serverside includes to get it.  document.getElementById(<%=chk.ClientID%>)

If you are getting started with ASP.Net a couple of places to go for info are:
http://www.asp.net/getstarted/default.aspx?tabid=61
http://samples.gotdotnet.com/quickstart/aspplus/

There are also a lot of great books on learning asp.net.  You can check them out on the asp.net website under the Resources -> Books. 

Good luck and let me know if there is anymore I can help you out with.

Jason
0
Maus
Top achievements
Rank 1
answered on 20 Oct 2006, 02:07 PM
No, no, no. It isn't ASP.NET that I'm just getting started with, but Javascript.

And the javascript resides on Masterpage.master, whereas chkExpire and ddlLength reside on edit_user.aspx.vb, this error is thrown:

Error 8 Name 'chkExpire' is not declared. C:\Inetpub\wwwroot\FTP\MasterPage.master 11 

Where line 11 is:

var chk = document.getElementById("<%=chkExpire.ClientID %>").checked

0
Jason Maronge
Top achievements
Rank 1
answered on 20 Oct 2006, 02:11 PM

Sorry about that.  You need to place the javascript on the page containing the controls. 

Jason

0
Maus
Top achievements
Rank 1
answered on 20 Oct 2006, 02:15 PM
If the head tags (and consequently the script tags) are located on the masterpage, how do I place javascript on the page itself?
0
Maus
Top achievements
Rank 1
answered on 20 Oct 2006, 02:15 PM
If the head tags (and consequently the script tags) are located on the masterpage, how do I place javascript on the page itself?
0
Jason Maronge
Top achievements
Rank 1
answered on 20 Oct 2006, 02:18 PM
Script tags to do have to just go in the head section.  You can place them anywhere on the page. 

Jason
0
Maus
Top achievements
Rank 1
answered on 20 Oct 2006, 02:24 PM
Ah.  That makes matters so much simpler.  Thank you!  I just tested it, and it works without a hitch.

Thanks again!
0
Cindy
Top achievements
Rank 1
answered on 08 Dec 2006, 03:10 PM
I have 2 RAD datepicker controls RadPatDOB & RadSecDOB.
My javascript code to set the value for one control with the value of another control does not work
Here is my code snippet

document.getElementById("<%=RadSecDOB.ClientID%>").value =document.getElementById("<%=RadPatDOB.ClientID%>").value;

The same problem with Maskedtextboxes.
What is the fix for this?

Thanks
Tags
Getting started with ASP.NET
Asked by
Maus
Top achievements
Rank 1
Answers by
Jason Maronge
Top achievements
Rank 1
Maus
Top achievements
Rank 1
Cindy
Top achievements
Rank 1
Share this question
or