dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Execute javascript in c# webbrowser

  1. #1
    Join Date
    Jan 2010
    Posts
    14

    Question Execute javascript in c# webbrowser

    Is it possible to run a javascript command through a c# web browser?
    Similarly to what most internet browsers can do, for example, like if I wanted to go to google.com then execute the code javascript:alert('testing') whilst on that page for an alert box to appear, how would I put the javascript into the c# program so that it runs when a button is pressed?

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Execute javascript in c# webbrowser

    Hi,

    Have a look at this link ClientScriptManager.RegisterClientScriptBlock. You can use the 'Attributes' collection of the button control to add handler for the 'onclick' client-side event.

  3. #3
    Join Date
    Jan 2010
    Posts
    14

    Re: Execute javascript in c# webbrowser

    when I try to use the code
    Code:
    if (!ClientScript.IsClientScriptBlockRegistered("mytest"))
                    ClientScript.RegisterClientScriptBlock(typeof(Page), "myrest", "<script>alert('Hello');</script>");
    I get the errors:
    Code:
    Error	2	The name 'ClientScript' does not exist in the current context	C:\Users\x\Documents\Visual Studio 2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs	27	18	WindowsFormsApplication4
    
    Error	4	The type or namespace name 'Page' could not be found (are you missing a using directive or an assembly reference?)	C:\Users\x\Documents\Visual Studio 2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs	29	63	WindowsFormsApplication4
    What is the assembly reference? I thought it would be using System.IO or using System.Net but neither of those worked.

  4. #4
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Execute javascript in c# webbrowser

    Visual studio can resolve namespaces for you. Right click on the Type that needs to be resolved, go to Resolve and add the namespace automatically, or explicitly.

    Edit: Also, the link you were given is to MSDN. MSDN gives you the namespace of any Type. The namespace you need is on the page you were given a link to.
    Last edited by mariocatch; February 1st, 2010 at 03:48 PM. Reason: Read MSDN.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)