javascript and .NET

11 posts, 0 answers
  1. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 19 Oct 2006 Link to this post

    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:
    1 function toggle_list()    {  
    2     var chk = document.forms[0].chkExpire.checked //document.getElementById("chkExpire")  
    3     var ddl = document.getElementById("ddlLength")  
    4     if (chk)  
    5         { ddl.disabled = true; }  
    6     else 
    7         { ddl.disabled = false; }  
    8     //return  
    9     } 

    and tie it in through the pageload in vb thus:
    1 If Not IsPostBack Then 
    2 'Code here edited out.  
    3 chkExpire.Attributes.Add("onClick""javascript: toggle_list();")  
    4 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?
  2. Jason Maronge
    Jason Maronge avatar
    322 posts
    Member since:
    Jan 2005

    Posted 19 Oct 2006 Link to this post

    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
  3. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 20 Oct 2006 Link to this post

    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?
  4. Jason Maronge
    Jason Maronge avatar
    322 posts
    Member since:
    Jan 2005

    Posted 20 Oct 2006 Link to this post

    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
  5. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 20 Oct 2006 Link to this post

    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

  6. Jason Maronge
    Jason Maronge avatar
    322 posts
    Member since:
    Jan 2005

    Posted 20 Oct 2006 Link to this post

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

    Jason

  7. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 20 Oct 2006 Link to this post

    If the head tags (and consequently the script tags) are located on the masterpage, how do I place javascript on the page itself?
  8. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 20 Oct 2006 Link to this post

    If the head tags (and consequently the script tags) are located on the masterpage, how do I place javascript on the page itself?
  9. Jason Maronge
    Jason Maronge avatar
    322 posts
    Member since:
    Jan 2005

    Posted 20 Oct 2006 Link to this post

    Script tags to do have to just go in the head section.  You can place them anywhere on the page. 

    Jason
  10. Maus
    Maus avatar
    21 posts
    Member since:
    Jul 2006

    Posted 20 Oct 2006 Link to this post

    Ah.  That makes matters so much simpler.  Thank you!  I just tested it, and it works without a hitch.

    Thanks again!
  11. Cindy
    Cindy avatar
    1 posts
    Member since:
    Dec 2006

    Posted 08 Dec 2006 Link to this post

    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
Back to Top