Retrieve Datagrid Element Index by Name

Discussion in 'ASP .Net Web Controls' started by John Walker, Nov 10, 2005.

  1. John Walker

    John Walker Guest

    Hi,
    I have two datagrids within one form and I need to do some client-side
    validation on the second grid when the user changes the value in drop down
    list which is a control in the second grid. For performance reasons I want
    javascript to loop through only the second grid when the drop down list event
    is fired, and in order to do this I had planned to put a hidden label between
    the two datagrids and have javasript start looping within the form using the
    hidden label as the starting point.
    I know how to get the form element names from the index(see code below), but
    need to know if I can get the element index from the element name. My plan
    is to get the index value of the hidden label and start looping from there.
    Please let me know if it is possible to get the index value from the element
    name and also if this seems like an ok plan.

    dml=document.Form1;
    len = dml.elements.length;
    var i = 0;
    while(i < len)
    {
    alert(dml.elements.name);
    i = i + 1;
    }

    Thanks, John
     
    John Walker, Nov 10, 2005
    #1
    1. Advertising

  2. If I attempt to get a collection of objects on an HTML page using JavaScript
    I would write a function like this:

    function FindControls(ControlID, TagName)
    {
    var ret= new Array();
    var aControls = document.getElementsByTagName(TagName);
    if (aControls)
    { for (var i=0; i < aControls.length ; i++)
    {
    if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
    ControlID.length)
    {
    ret.push(aControls);
    }
    }
    }
    return ret;
    }


    Which you can call for any type of objects by passing the controlID and the
    tag type:
    function DisplayListsWithID_ddlList()
    {

    var controls = FindControls("ddlList","select");
    alert(controls.length); //this gives how many on the page
    for (var i=0;i< controls.length;i++)
    {
    //this allows you to loop through them and
    //process any thing on them
    alert(controls.id);
    }

    }

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "John Walker" wrote:

    > Hi,
    > I have two datagrids within one form and I need to do some client-side
    > validation on the second grid when the user changes the value in drop down
    > list which is a control in the second grid. For performance reasons I want
    > javascript to loop through only the second grid when the drop down list event
    > is fired, and in order to do this I had planned to put a hidden label between
    > the two datagrids and have javasript start looping within the form using the
    > hidden label as the starting point.
    > I know how to get the form element names from the index(see code below), but
    > need to know if I can get the element index from the element name. My plan
    > is to get the index value of the hidden label and start looping from there.
    > Please let me know if it is possible to get the index value from the element
    > name and also if this seems like an ok plan.
    >
    > dml=document.Form1;
    > len = dml.elements.length;
    > var i = 0;
    > while(i < len)
    > {
    > alert(dml.elements.name);
    > i = i + 1;
    > }
    >
    > Thanks, John
    >
     
    Phillip Williams, Nov 10, 2005
    #2
    1. Advertising

  3. John Walker

    John Walker Guest

    Phillip,

    It looks like FindControls will return an array of controls which have the
    specified tag name. I'm not sure if that's what I need. I think what I need
    is ability to loop through all the elements in only one of two tables in a
    form. If Form1 has two tables, Table1 and Table2, is there a way to tell
    javascript to only loop through Table2 when we're doing our validation? I
    need to skip Table1 altogether because of performance issues.

    Thanks again,
    John

    "Phillip Williams" wrote:

    > If I attempt to get a collection of objects on an HTML page using JavaScript
    > I would write a function like this:
    >
    > function FindControls(ControlID, TagName)
    > {
    > var ret= new Array();
    > var aControls = document.getElementsByTagName(TagName);
    > if (aControls)
    > { for (var i=0; i < aControls.length ; i++)
    > {
    > if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
    > ControlID.length)
    > {
    > ret.push(aControls);
    > }
    > }
    > }
    > return ret;
    > }
    >
    >
    > Which you can call for any type of objects by passing the controlID and the
    > tag type:
    > function DisplayListsWithID_ddlList()
    > {
    >
    > var controls = FindControls("ddlList","select");
    > alert(controls.length); //this gives how many on the page
    > for (var i=0;i< controls.length;i++)
    > {
    > //this allows you to loop through them and
    > //process any thing on them
    > alert(controls.id);
    > }
    >
    > }
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "John Walker" wrote:
    >
    > > Hi,
    > > I have two datagrids within one form and I need to do some client-side
    > > validation on the second grid when the user changes the value in drop down
    > > list which is a control in the second grid. For performance reasons I want
    > > javascript to loop through only the second grid when the drop down list event
    > > is fired, and in order to do this I had planned to put a hidden label between
    > > the two datagrids and have javasript start looping within the form using the
    > > hidden label as the starting point.
    > > I know how to get the form element names from the index(see code below), but
    > > need to know if I can get the element index from the element name. My plan
    > > is to get the index value of the hidden label and start looping from there.
    > > Please let me know if it is possible to get the index value from the element
    > > name and also if this seems like an ok plan.
    > >
    > > dml=document.Form1;
    > > len = dml.elements.length;
    > > var i = 0;
    > > while(i < len)
    > > {
    > > alert(dml.elements.name);
    > > i = i + 1;
    > > }
    > >
    > > Thanks, John
    > >
     
    John Walker, Nov 11, 2005
    #3
  4. Hello John,

    When you say "all of the elements" you probably have different processes to
    apply to different controls, i.e. one process for dropdownlists, one for
    tablecells and so on. If you have dropdownlists in table2 that you gave a
    specific ID then you can find them first using the FindControls function then
    do another set of controls of the same type and so on. The FindControls
    functions (below) only picks up a set of controls that have the same ID (I
    did it specifically this way to fit a templated control that emits the same
    control as many times as there are items).

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "John Walker" wrote:

    > Phillip,
    >
    > It looks like FindControls will return an array of controls which have the
    > specified tag name. I'm not sure if that's what I need. I think what I need
    > is ability to loop through all the elements in only one of two tables in a
    > form. If Form1 has two tables, Table1 and Table2, is there a way to tell
    > javascript to only loop through Table2 when we're doing our validation? I
    > need to skip Table1 altogether because of performance issues.
    >
    > Thanks again,
    > John
    >
    > "Phillip Williams" wrote:
    >
    > > If I attempt to get a collection of objects on an HTML page using JavaScript
    > > I would write a function like this:
    > >
    > > function FindControls(ControlID, TagName)
    > > {
    > > var ret= new Array();
    > > var aControls = document.getElementsByTagName(TagName);
    > > if (aControls)
    > > { for (var i=0; i < aControls.length ; i++)
    > > {
    > > if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
    > > ControlID.length)
    > > {
    > > ret.push(aControls);
    > > }
    > > }
    > > }
    > > return ret;
    > > }
    > >
    > >
    > > Which you can call for any type of objects by passing the controlID and the
    > > tag type:
    > > function DisplayListsWithID_ddlList()
    > > {
    > >
    > > var controls = FindControls("ddlList","select");
    > > alert(controls.length); //this gives how many on the page
    > > for (var i=0;i< controls.length;i++)
    > > {
    > > //this allows you to loop through them and
    > > //process any thing on them
    > > alert(controls.id);
    > > }
    > >
    > > }
    > >
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "John Walker" wrote:
    > >
    > > > Hi,
    > > > I have two datagrids within one form and I need to do some client-side
    > > > validation on the second grid when the user changes the value in drop down
    > > > list which is a control in the second grid. For performance reasons I want
    > > > javascript to loop through only the second grid when the drop down list event
    > > > is fired, and in order to do this I had planned to put a hidden label between
    > > > the two datagrids and have javasript start looping within the form using the
    > > > hidden label as the starting point.
    > > > I know how to get the form element names from the index(see code below), but
    > > > need to know if I can get the element index from the element name. My plan
    > > > is to get the index value of the hidden label and start looping from there.
    > > > Please let me know if it is possible to get the index value from the element
    > > > name and also if this seems like an ok plan.
    > > >
    > > > dml=document.Form1;
    > > > len = dml.elements.length;
    > > > var i = 0;
    > > > while(i < len)
    > > > {
    > > > alert(dml.elements.name);
    > > > i = i + 1;
    > > > }
    > > >
    > > > Thanks, John
    > > >
     
    Phillip Williams, Nov 11, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Walzer
    Replies:
    3
    Views:
    1,107
    Hendrik van Rooyen
    Dec 23, 2006
  2. HANM
    Replies:
    2
    Views:
    750
    Joseph Kesselman
    Jan 29, 2008
  3. agora1983
    Replies:
    0
    Views:
    658
    agora1983
    Feb 11, 2008
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    336
    Tomasz Chmielewski
    Mar 4, 2008
  5. Replies:
    6
    Views:
    195
Loading...

Share This Page