Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Retrieve Datagrid Element Index by Name

Reply
Thread Tools

Retrieve Datagrid Element Index by Name

 
 
John Walker
Guest
Posts: n/a
 
      11-10-2005
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[i].name);
i = i + 1;
}

Thanks, John

 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      11-10-2005
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[i].id.lastIndexOf(ControlID) == aControls[i].id.length -
ControlID.length)
{
ret.push(aControls[i]);
}
}
}
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[i].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[i].name);
> i = i + 1;
> }
>
> Thanks, John
>

 
Reply With Quote
 
 
 
 
John Walker
Guest
Posts: n/a
 
      11-11-2005
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[i].id.lastIndexOf(ControlID) == aControls[i].id.length -
> ControlID.length)
> {
> ret.push(aControls[i]);
> }
> }
> }
> 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[i].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[i].name);
> > i = i + 1;
> > }
> >
> > Thanks, John
> >

 
Reply With Quote
 
Phillip Williams
Guest
Posts: n/a
 
      11-11-2005
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[i].id.lastIndexOf(ControlID) == aControls[i].id.length -
> > ControlID.length)
> > {
> > ret.push(aControls[i]);
> > }
> > }
> > }
> > 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[i].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[i].name);
> > > i = i + 1;
> > > }
> > >
> > > Thanks, John
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
Retrieve the data from the GridViewRow by column name instead of index agora1983 ASP .Net 0 02-11-2008 02:52 PM
how can i retrieve the name of form when one of the element name in this form is 'name' ashok.dhananjeyan@gmail.com Javascript 6 11-24-2006 02:54 AM
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Stefan ASP .Net Datagrid Control 2 07-31-2004 05:52 AM



Advertisments