Getting form NameValueCollection from Html

N

Nicholas Paldino [.NET/C# MVP]

Ofir,

Are you using this in ASP.NET? This property is offered through the
Page object (which exposes it through the Request object on the Page), or
you can get it from the static Current property on the HttpContext class
(which will return the current context, which exposes the HttpRequest
instance through the Request property, which you can access the Form
property through).
 
O

ofiras

Thanks for explaining,
I'm not using asp.net, so can I get the form info in some another way
that can be used in .NET?
Thanks,
Ofir.
 
N

Nicholas Paldino [.NET/C# MVP]

Ofir,

If you aren't in ASP.NET, are you trying to get the actual values on a
form in an HTML document that is in the browser? If so, you are going to
have to connect to the browser to get the values from the document object
model.

Where exactly are you trying to get the values from?
 
O

ofiras

I want to get the values from a web page on the net.
I have its URL, and I know how to get his html with WebClient, so a
method that takes html code and returns all the forms names and
fields.
I'll be happy if I will get their place in the html code, but I can do
it myself so it's not important.
Thanks,
Ofir.
 
N

Nicholas Paldino [.NET/C# MVP]

Ofir,

If that is the case, once you have downloaded the HTML, you should feed
it into a DOM (Document Object Module) parser and get the object model.
Then, you look for the FORM elements, and the INPUT elements that are
children of that.
 
M

Marc Gravell

I guess I need System.CodeDom
CodeDom is something completely unrelated - don't go near that for
this...
I need to use HtmlPage class with using System.Windows.Browser
Well, System.Windows.Forms.WebBrowser may be a good equivalent; this
wraps ShDocVw, so you get the same DOM as IE / DHTML uses... but other
DOMs are available for HTML. But if WebBrowser works for you...

Marc
 
M

Marc Gravell

Here's an example using WebBrower; other DOMs are available that don't
rely on the UI-based WebBrowser (ShDocVw):

using System;
using System.Windows.Forms;
using System.Diagnostics;

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (Form f = new Form())
using (WebBrowser wb = new WebBrowser())
{
f.Controls.Add(wb);
f.Load += delegate
{
wb.Navigate("http://www.google.com");
};
wb.DocumentCompleted += delegate
{
foreach (HtmlElement el in wb.Document.Forms)
{
Trace.WriteLine("Found form: " + el.Id + "/" +
el.Name);
}
};
Application.Run(f);
}

}

}
 
O

ofiras

One more thing - I'm trying to create click event on the form input,
but it doesn't work.
It looks like:

foreach (HtmlElement el in webBrowser1.Document.Forms)
{
foreach (HtmlElement cel in el.Children)
{
cel.Click += delegate
{
MessageBox.Show(cel.OuterHtml);
};
}
}

But when I click it, it shows me the html of the last child, and not
the one I pressed on.
I tried it with for too, but it didn't work either.
How can I make it do it for the one I clicked on, and not the last
one?
Please help,
Ofir.
 
N

Nicholas Paldino [.NET/C# MVP]

Ofir,

This is because of the way that anonymous methods work. You have to
assign cel to another variable in order to have a different anonymous
delegate created each time:

foreach (HtmlElement el in webBrowser1.Document.Forms)
{
foreach (HtmlElement cel in el.Children)
{
HtmlElement cel2 = cel;

cel.Click += delegate
{
MessageBox.Show(cel2.OuterHtml);
};
}
}

The original code was creating one delegate with the first assignment of
cel.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top