1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Asp + Js

Discussion in 'C#' started by connieyongg, Sep 14, 2006.

  1. #1
    Hi Experts,

    I'm trying to develop a dynamically develop function, at the same time i need to include some ASP command. herein below is my sample code ( DOES NOT WORK )

    <body>

    <form action="" method="" name="formname">

    <%

    For x = 1 to RS.RecordCount AND Y=1 to RS.RecordCount

    %>

    <Script>

    function codename() {

    if(document.formname.<%=y%>.checked)
    {
    document.formname.<%=x%>.disabled=false;
    }
    else
    {
    document.formname.<%=x%>.disabled=true;
    }
    }
    //-->
    </SCRIPT>


    <input type="text" disabled size="10" name="<%=x%>">
    <input type="checkbox" onclick="codename()" name="<%=y%>" value="ON">

    </body>

    <%
    RS.MoveNext
    NEXT
    %>


    please assist and appreciate your kind advice.


    Thank you in advance
     
    connieyongg, Sep 14, 2006 IP
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Connie, welcome to DP!

    This is a good idea, but it's riddled with problems. it ain't just one. but since i haven't posted here in a while, i'll take this one just for kicks. here's my take on why it isnt working:

    1. JS form field names can't begin (or be) numbers. they can be alphanumeric, but can't begin with a number.

    2. your for... next statement is wonky. i don't believe for... next works quite like this, someone correct me if i'm wrong. anyways, regardless, why over-complicate things? if x = 1 to rs.recordcount and y = the same thing, why not just use one? a good rule of thumb: "K. I. S. S."

    3. you might think about defining your script language, i.e. <SCRIPT LANGUAGE="JavaScript">.

    4. your body tag is inside your loop... (EEK!) this, obviously, is a big no-no. body tag after all loops unless you have a reason, and in this case, you don't.

    5. <rant>close your tags!!!</rant> for further reading, google "</form>". also be sure to put this one outside of your loop (see above suggestion) but obviously inside of your body tag.

    6. you are essentially defining and redefining and redefining ad nauseum the javascript function "codename". won't work. you'll need to either set up seperate functions for each element or write a function that works dynamically, regardless of the form field name. i'm gonna take the first option, though it's not the best, it's the easiest since it's essentially what you've already got so far, and i'm just not going to wrestle with changing all the logic. Setting up a dynamic function to handle all is the better way to go if you have the time to screw with it.

    8. for... next should work without being stepped up (RS.MoveNext). besides you're stepping up x, not rs. unless you're doing something else with your recordset, in which case it might work better to use a different kind of loop for the job. like "while not RS.eof... wend" or something. your call.

    7. while you're at it, why not focus the text field on checkbox click.

    ok here goes:

    
    <body>
    
    <form action="" method="" name="formname">
    
    <%
    
    For x = 1 to RS.RecordCount
    
    %>
    
    <SCRIPT LANGUAGE="JavaScript">
    function codename<%=x%>() {
    
    if(document.formname.check<%=x%>.checked) 
    {
    document.formname.field<%=x%>.disabled=false; 
    document.formname.field<%=x%>.focus();
    }
    else
    {
    document.formname.field<%=x%>.disabled=true;
    }
    }
    
    </SCRIPT>
    
    
    <input type="text" onfocus="codename<%=x%>()" size="10" name="field<%=x%>">
    <input type="checkbox" onclick="codename<%=x%>()" name="check<%=x%>" value="ON">
    
    <%
    
    NEXT
    %>
    </form>
    </body>
    
    
    Code (markup):
    it should work. if it doesn't, to see it working properly, change "For x = 1 to RS.RecordCount" to "For x = 1 to 10". if it isn't working with your RS.Recordcount, do a response.write and see if you're actually getting a variable for RS.RecordCount.

    ok that's my quota. GL

    VG
     
    vectorgraphx, Sep 14, 2006 IP