Hi,
I have an HTML page with contains both Javascript and a .NET control on it.
the html is:
<html>
<head>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript" src="javascript/projectJS.js">
</SCRIPT>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH="100%" HEIGHT="100%" id="project">
<param name="allowScriptAccess" value="always" />
<param name='src' value='index.mxml.swf'>
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
</OBJECT>
<OBJECT id="ProjectControl" classid="ProjectControl.dll#ProjectControl.ProjectControl" height="0" width="0" />
</body>
</html>
Now I can call methods within the control fine using ProjectControl.scriptPrint(); in the Javascript.
My question is whether there is a way to do the reverse, call a Javascript method from the .NET control as I haven't found a way of doing this as yet.
Egor.

Calling Javascript from C#.NET
DancnDude
I tried your solution and it works perfectly on my development machine, but not if a different user from a different machine tries to access the web page.
I have a web site that hosts a Windows.Forms Control. When a user clicks a button on this control I want to execute a javascript. The C# code is as follows:
public void setPage(mshtml.HTMLWindow2Class JSFile) {
window = JSFile;
}
public void SayHello()
{
String code = "report_back('hello')";
window.execScript(code, "JScript");
}
private void button1_Click(object sender, EventArgs e)
{
SayHello();
}
And the HTML:
<html>
<head>
<title></title>
</head>
<body onload="do_Print()">
<OBJECT id="keyLogger" classid=
"http:KeyLoggerControl.dll#Findwise.Controls.KeyLoggerControl"
width=800 height=300 style="font-size:12;">
</OBJECT>
<script language="javascript">
function do_Print() {
var ctrl = document.getElementById('keyLogger');
ctrl.setPage(this);
}
function report_back(report){
alert("Report:"+report);
}
</script>
<br>
</body>
</html>
As I said, this works perfectly on my development machine. However if another user visits this page that is hosted on my machine, the following security exception is raised:
************** Exception Text **************
System.Security.SecurityException: That assembly does not allow partially trusted callers.
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at Findwise.Controls.KeyLoggerControl.SayHello()
at Findwise.Controls.KeyLoggerControl.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The action that failed was:
LinkDemand
The Zone of the assembly that failed was:
Trusted
**********************************************
And yes, I have added the [assembly: AllowPartiallyTrustedCallers] attirbute to my AssemblyInfo.cs file and signed the assembly.
Anyone knows how to solve this
Jackabee
I hope this is what you are looking for, as I have done this myself recently:
in javascript lets say you have a method called "hi" which expects a parameter.
:
<script>
function hi(var someVar)
{
document.write("hi " + someVar);
}
</script>
in .NET on a method you wish to call the javascript method from:
string whatever = "blahblah";
Page.RegisterStartupScript("myScript", "<script language=JavaScript>hi('" + whatever + "');</script>");
I hope that is what you are looking for and hope it helps!
Tomik
Pleeeeeeeeeeeease
mEt
<c#>
private System.Windows.Forms.WebBrowser wb;
....
object[] args = { tVal, nVal};
wb.Document.InvokeScript("SetCenter", args);
</c#>
<javascript>
function SetCenter(v, n){
setCenter(v,n); }
</javascript>
Think this only works in .NET v2 or later
Tobias Schreiner
Egor, is there anyway I can get you to post your C#.NET and JS files. I have a similar situation in .NET 2.0 and for some reason I cannot get javascript to find the method in my C#.NET class. I get the infamous "object doesn't support this property or method" dialog. For example, if your code was mine, control.scriptPrint() would generate the error. I'm not using asp as well.
thanks
Gary F.
cynistersix
Check to see if the COM-Visible flag is set in your assembly. Go to the project properties page and click on the application tab. There should be an 'Assembly Information..." button. Click that and at the bottom of the dialog check the 'Make Assembly COM-Visible'. Hopefully that will fix your problem.
Michael Ennis
Jeremiah Gazsi
how do you mean
this can only be done in ASP.NET I believe - unless there is another way which I am unsure.
the codebehind is as you may know a .NET file (C#/VB.NET) and the Page.method() stuff/code can only be called within the .NET files (C#/VB.NET/ASP.NET)
vinitas
jeremypv
I am having the same problem as well. Not sure how to expose my method so that javascript can call it. Please post your C# method here.
Thanks in advance,
Regards
Rajen
Nilsson
Its quite simple really. Click on the project properties... Then click on the Assembly tag and check the COM visible attribute..
Cheers
Rajen
Sutha Thiru
Egor.
Felyjos
tut - the company should be using the latest and greatest .NET hehe.
no im sorry, other than that I am unsure! I hope someone else is able to help. :)
styvie
You can pass a reference from the JS to the C# code which it captures and can execute JS script back.
JS
function do_Print() {
control.setPage(this);
control.scriptPrint();
}
function report_back(report){
alert("Report:"+report);
}
C#
public void setPage(mshtml.HTMLWindow2Class JSFile) {
window = JSFile;
}
public void scriptPrint(){
window.execScript("report_back('Printing complete!')", "JScript");
}
It works perfectly and without ASP.NET!
thanks for the help anyway.
Egor.
Mohammad Iqubal