Home All Groups Group Topic Archive Search About

Getting form NameValueCollection from Html

Author
23 Nov 2007 1:06 PM
ofiras
Hello,
I need to check for forms in html on the web, and I found this:
http://msdn2.microsoft.com/en-us/library/system.web.httprequest.form(VS.90).aspx
But I can't find this property in C#...
Am I doing something wrong? Is there something else that dose it?
If not, I guess I'll need to make my own method that will do it.
Please help,
Ofir.

Author
23 Nov 2007 1:48 PM
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).


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quote
"ofiras" <ofi***@gmail.com> wrote in message
news:1912660e-8663-4cd2-a2e4-2cf7938db483@f3g2000hsg.googlegroups.com...
> Hello,
> I need to check for forms in html on the web, and I found this:
> http://msdn2.microsoft.com/en-us/library/system.web.httprequest.form(VS.90).aspx
> But I can't find this property in C#...
> Am I doing something wrong? Is there something else that dose it?
> If not, I guess I'll need to make my own method that will do it.
> Please help,
> Ofir.
Author
23 Nov 2007 3:31 PM
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.
Author
23 Nov 2007 4:36 PM
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?


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quote
"ofiras" <ofi***@gmail.com> wrote in message
news:723bf075-7446-4eed-888b-252020b4c1a4@s12g2000prg.googlegroups.com...
> 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.
Author
23 Nov 2007 5:05 PM
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.
Author
24 Nov 2007 4:10 AM
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.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quote
"ofiras" <ofi***@gmail.com> wrote in message
news:dc166f6f-0679-47aa-a1c4-ac77857fab61@s36g2000prg.googlegroups.com...
>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.
Author
24 Nov 2007 9:45 AM
ofiras
Thanks,
But I don't know how to use Dom... I guess I need System.CodeDom, but
how do I get the form elements from it?
As I saw in this article: http://blogs.msdn.com/tims/archive/2007/06/13/programming-html-with-c.aspx
I need to use HtmlPage class with using System.Windows.Browser, but
the C# doesn't have this namespace.
Please help,
Ofir.
Author
24 Nov 2007 9:49 AM
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
Author
24 Nov 2007 2:08 PM
ofiras
I don't get it...
How do I make DOM?
Author
24 Nov 2007 10:30 PM
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);
        }

    }

}
Author
25 Nov 2007 1:46 PM
ofiras
Thenks,
Got it.
Author
25 Nov 2007 2:30 PM
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.
Author
25 Nov 2007 2:45 PM
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.

--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quote
"ofiras" <ofi***@gmail.com> wrote in message
news:71a6be20-c615-4a0d-a2c9-5d6c79af5245@d4g2000prg.googlegroups.com...
> 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.

AddThis Social Bookmark Button