Connecting Tech Pros Worldwide Forums | Help | Site Map

How to loop through FORM elements?

Brett
Guest
 
Posts: n/a
#1: Nov 17 '05
The following code will allow me to loop through FORM tags but not the
elements in them. I may have five forms on one page. How do I loop through
form elements in the forth FORM?

private SHDocVw.InternetExplorer IE_Inst = new
SHDocVw.InternetExplorerClass();
.....
mshtml.IHTMLDocument2 HTMLDocument =
(mshtml.IHTMLDocument2) this.IE_Inst.Document;
mshtml.IHTMLElementCollection forms = HTMLDocument.forms;

foreach (mshtml.HTMLFormElementClass el in forms)
{

strType = el.outerHTML;
}

Once I'm in a particular FORM, I want to find certain input fields and fill
in values.

Thanks,
Brett



Brett
Guest
 
Posts: n/a
#2: Nov 17 '05

re: How to loop through FORM elements?



"Brett" <no@spam.com> wrote in message
news:OUFIqAmUFHA.3292@TK2MSFTNGP14.phx.gbl...[color=blue]
> The following code will allow me to loop through FORM tags but not the
> elements in them. I may have five forms on one page. How do I loop
> through form elements in the forth FORM?
>
> private SHDocVw.InternetExplorer IE_Inst = new
> SHDocVw.InternetExplorerClass();
> ....
> mshtml.IHTMLDocument2 HTMLDocument =
> (mshtml.IHTMLDocument2) this.IE_Inst.Document;
> mshtml.IHTMLElementCollection forms = HTMLDocument.forms;
>
> foreach (mshtml.HTMLFormElementClass el in forms)
> {
>
> strType = el.outerHTML;
> }
>
> Once I'm in a particular FORM, I want to find certain input fields and
> fill in values.
>
> Thanks,
> Brett
>[/color]

I'm currently getting around the problem by looping through all elements in
the Doc:

foreach (mshtml.IHTMLElement wbrElm in wbrAll)
{
// Assign the inner html values of the input to our variables
//look for only INPUT types
if(wbrElm.tagName.ToLower() == "input" &&
wbrElm.outerHTML.IndexOf("name", 1) > 0)
{
strName = wbrElm.getAttribute("name", 0).ToString();
Debug.WriteLine(wbrElm.tagName.ToLower() + " -- " + wbrElm.outerHTML);
// We are only interested in filling text boxes,
// and only interested in a specific one, i.e. "q"

if (strName != null && strName.ToLower() == userNameFormField)
// Set the "value" with the setAttribute method.
wbrElm.setAttribute("value", userNameValue, 0);


if (strName != null && strName.ToLower() == passwordFormField)
// Set the "value" with the setAttribute method.
wbrElm.setAttribute("value", passwordValue, 0);
}
} //end for each

Not the best technique.

Brett


if (strName != null && strName.ToLower() == passwordFormField)
// Set the "value" with the setAttribute method.
wbrElm.setAttribute("value", passwordValue, 0);
}
} //end for each


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#3: Nov 17 '05

re: How to loop through FORM elements?


Hi,

Is this in the client side? if so you better use javascript , this code
could give you an idea

for(int i =0 ; i < document.forms[3].elements.length; i ++ )
alert( document.forms[3].elements[i].tagName );

note that I did not test it, so it may have errors


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




"Brett" <no@spam.com> wrote in message
news:ubCEmbmUFHA.3840@tk2msftngp13.phx.gbl...[color=blue]
>
> "Brett" <no@spam.com> wrote in message
> news:OUFIqAmUFHA.3292@TK2MSFTNGP14.phx.gbl...[color=green]
>> The following code will allow me to loop through FORM tags but not the
>> elements in them. I may have five forms on one page. How do I loop
>> through form elements in the forth FORM?
>>
>> private SHDocVw.InternetExplorer IE_Inst = new
>> SHDocVw.InternetExplorerClass();
>> ....
>> mshtml.IHTMLDocument2 HTMLDocument =
>> (mshtml.IHTMLDocument2) this.IE_Inst.Document;
>> mshtml.IHTMLElementCollection forms = HTMLDocument.forms;
>>
>> foreach (mshtml.HTMLFormElementClass el in forms)
>> {
>>
>> strType = el.outerHTML;
>> }
>>
>> Once I'm in a particular FORM, I want to find certain input fields and
>> fill in values.
>>
>> Thanks,
>> Brett
>>[/color]
>
> I'm currently getting around the problem by looping through all elements
> in the Doc:
>
> foreach (mshtml.IHTMLElement wbrElm in wbrAll)
> {
> // Assign the inner html values of the input to our variables
> //look for only INPUT types
> if(wbrElm.tagName.ToLower() == "input" &&
> wbrElm.outerHTML.IndexOf("name", 1) > 0)
> {
> strName = wbrElm.getAttribute("name", 0).ToString();
> Debug.WriteLine(wbrElm.tagName.ToLower() + " -- " +
> wbrElm.outerHTML);
> // We are only interested in filling text boxes,
> // and only interested in a specific one, i.e. "q"
>
> if (strName != null && strName.ToLower() == userNameFormField) // Set
> the "value" with the setAttribute method.
> wbrElm.setAttribute("value", userNameValue, 0);
>
>
> if (strName != null && strName.ToLower() == passwordFormField) // Set
> the "value" with the setAttribute method.
> wbrElm.setAttribute("value", passwordValue, 0);
> }
> } //end for each
>
> Not the best technique.
>
> Brett
>
>
> if (strName != null && strName.ToLower() == passwordFormField) // Set
> the "value" with the setAttribute method.
> wbrElm.setAttribute("value", passwordValue, 0);
> }
> } //end for each
>[/color]


Closed Thread