TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

Web Browser Control in Visual Studio 2005

How cool is this? As a fan of Binary DHTML behaviors, I gotta say that the new Web Browser control for WinForms in Visual Studio 2005 looks pretty slick so far! Below I have a code snippet that I'm using to build a control dynamically, attach (and detach) an event, and then control the HTML document within...

HtmlElement button = null;

 

private void Form1_Load(object sender, EventArgs e) {

    this.webBrowser1.Navigate("about:blank");

    this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_Initialize);

   

}

 

private void webBrowser1_Initialize(object sender, WebBrowserDocumentCompletedEventArgs e) {

    this.webBrowser1.DocumentCompleted -= this.webBrowser1_Initialize;

    this.webBrowser1.Document.Body.InnerHtml = "<h1>Hello World!</h1>";

    button = this.webBrowser1.Document.CreateElement("input");

    button.SetAttribute("type", "button");

    button.SetAttribute("value", "GoTo Google");

    this.webBrowser1.Document.Body.AppendChild(button);

    button.Click +=new HtmlElementEventHandler(button_Click);

}

 

private void button_Click(object sender, EventArgs e) {

    this.webBrowser1.Navigate("http://www.google.com");

}


Digg!

posted on Saturday, March 19, 2005 1:34 PM

Feedback

# re: Web Browser Control in Visual Studio 2005 9/27/2006 10:04 PM Asiya

when i wanna generate table containing some value and a button...on button press i need to trigger onclick event ..i hav written following code

..............

HtmlElement tableCell2 = doc.CreateElement("TD");
HtmlElement newbutton = doc.CreateElement ("input");
newbutton.SetAttribute("type", "button");
newbutton.SetAttribute("value", "Edit");
newbutton.SetAttribute("id", "mybutton_" + ind.ToString());
newbutton.SetAttribute("onclick", "alert();");
ind += 1;
tableCell2.AppendChild(newbutton);


If i copy the HTML generated by this code, means use static HTML, event does fire and things work somoothly but i generate that dynamically as i m doing above, event does not get fired...any suggestion