Calling JavaScript from C# code.

Asked By 0 points N/A Posted on -
qa-featured

I want to call JavaScript code from C#.

I have used RegisterClientScriptBlock method to do so.

But now the problem is that this method is being called in a loop.

So, it is executed only once.

I want this code to be executed on every iteration.

How can I achieve this?

SHARE
Answered By 0 points N/A #92812

Calling JavaScript from C# code.

qa-featured

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!

But if you want to use the way that you are already using, then I would suggest you to contact the indexer with script title in page client script block method. In this way, every time the code will be registered with a new name so it will be executed. Otherwise,it executes only once.

Login/Register to Answer

Related Questions