How to get a title of a page displayed in webbrowser control in c# ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jon86
    New Member
    • Jul 2008
    • 8

    How to get a title of a page displayed in webbrowser control in c# ?

    Hi All ,
    Any one please help me in getting a title of a page displayed in webbrowser control in c# .The title needs to be displayed in lable in a windows form which has this webcontrol intit.I tried with HTMLTitleElemen tClass but it's not returning anything.
    Then I tried with DocumentTitle property which should display the title of the document currently displayed in the WebBrowser control. But Hard luck ... This also failed.

    Any Help in this is appreciated.

    Cheers,
    Jon
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    This did not work?
    Code:
    myWebbrowser.Document.Title

    Comment

    • Jon86
      New Member
      • Jul 2008
      • 8

      #3
      Originally posted by Plater
      This did not work?
      Code:
      myWebbrowser.Document.Title
      I already tried Document.Title. .. HARD LUCK ...

      Please help

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Are you sure the document HAS a title?

        Comment

        • Daxthecon
          New Member
          • Jul 2008
          • 63

          #5
          Are you using an ASP.Net Front end or an empty Web Site?


          btw Plater and Alias this forum would be lost without you both. Plus I like seeing the preformatted MODERATOR messages.

          Comment

          • Jon86
            New Member
            • Jul 2008
            • 8

            #6
            Originally posted by Plater
            Are you sure the document HAS a title?
            Yes of course the page is having a title .... It's not showing title for any sites/page.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Weird. I just ran it and it gave me the title just fine.
              You are waiting until the Navigated event has fired before looking for a title right?

              Here's what I just tried.
              [code=c#]
              private bool cango = false;
              private string GetTitle(string url)
              {
              string retval = "";
              cango = false;
              myWebBrowser.Na vigate(url);
              while (!cango)
              {
              Application.DoE vents();
              }
              cango = false;
              retval = myWebBrowser.Do cument.Title;
              return retval;
              }

              void wb_Navigated(ob ject sender, WebBrowserNavig atedEventArgs e)
              {
              cango = true;
              }
              [/code]

              Now since you're actually using the webpage, I would just do this:
              [code=c#]
              void wb_Navigated(ob ject sender, WebBrowserNavig atedEventArgs e)
              {
              myLabel.Text=my WebBrowser.Docu ment.Title;
              }
              [/code]

              Comment

              • Jon86
                New Member
                • Jul 2008
                • 8

                #8
                Originally posted by Daxthecon
                Are you using an ASP.Net Front end or an empty Web Site?


                btw Plater and Alias this forum would be lost without you both. Plus I like seeing the preformatted MODERATOR messages.

                The title needs to be displayed in label in a windows form which has this webcontrol in it

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  Originally posted by Daxthecon
                  btw Plater and Alias this forum would be lost without you both. Plus I like seeing the preformatted MODERATOR messages.
                  Why thank you! Nice to know that we are appreciated
                  :D

                  Comment

                  • Jon86
                    New Member
                    • Jul 2008
                    • 8

                    #10
                    Originally posted by Plater
                    Weird. I just ran it and it gave me the title just fine.
                    You are waiting until the Navigated event has fired before looking for a title right?

                    Here's what I just tried.
                    [code=c#]
                    private bool cango = false;
                    private string GetTitle(string url)
                    {
                    string retval = "";
                    cango = false;
                    myWebBrowser.Na vigate(url);
                    while (!cango)
                    {
                    Application.DoE vents();
                    }
                    cango = false;
                    retval = myWebBrowser.Do cument.Title;
                    return retval;
                    }

                    void wb_Navigated(ob ject sender, WebBrowserNavig atedEventArgs e)
                    {
                    cango = true;
                    }
                    [/code]

                    Now since you're actually using the webpage, I would just do this:
                    [code=c#]
                    void wb_Navigated(ob ject sender, WebBrowserNavig atedEventArgs e)
                    {
                    myLabel.Text=my WebBrowser.Docu ment.Title;
                    }
                    [/code]



                    Hi,
                    This is what I tried with the piece of code you send .....still the label is blank ...
                    Can you please guide me on this...

                    [code=c#]
                    using System;
                    using System.Collecti ons.Generic;
                    using System.Componen tModel;
                    using System.Data;
                    using System.Drawing;
                    using System.Text;
                    using System.Windows. Forms;

                    namespace testwinform
                    {
                    public partial class Form1 : Form
                    {
                    public Form1()
                    {
                    InitializeCompo nent();
                    }

                    private void button1_Click(o bject sender, EventArgs e)
                    {
                    string url=textBox1.Te xt;
                    GetTitle(url);


                    }

                    private bool cango = false;
                    private string GetTitle(string url)
                    {
                    string retval = "";
                    cango = false;
                    webBrowser1.Nav igate(url);
                    while (!cango)
                    {
                    Application.DoE vents();
                    }
                    cango = false;
                    retval = webBrowser1.Doc ument.Title;
                    return retval;
                    }

                    void wb_Navigated(ob ject sender, WebBrowserNavig atedEventArgs e)
                    {
                    cango = true;

                    label1.Text = webBrowser1.Doc ument.Title;
                    }



                    }
                    }
                    [/code]


                    Appreciate your help,
                    Jon
                    Last edited by Curtis Rutland; Jul 17 '08, 07:43 PM. Reason: Added Code Tags - Please use the # button

                    Comment

                    • Curtis Rutland
                      Recognized Expert Specialist
                      • Apr 2008
                      • 3264

                      #11
                      Please use the [code] tags when posting your code. It makes it easier for the experts to read, and the easier, the better and faster you get help. Please read the Posting Guidelines.

                      MODERATOR

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Did you remember to attach that wb_navigated handler function to the navigated event on your webbrowser control?

                        Comment

                        • Jon86
                          New Member
                          • Jul 2008
                          • 8

                          #13
                          Originally posted by Plater
                          Did you remember to attach that wb_navigated handler function to the navigated event on your webbrowser control?
                          yes buddy .. i created the navigated event handler ..

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Originally posted by Jon86
                            yes buddy .. i created the navigated event handler ..
                            Well then I don't know what to tell you. You must be doing something different somewhere.
                            I did it in like 3mins and it worked just fine.

                            Comment

                            Working...