Home > Net >  Background of js c # code calls the front desk page could not be copied
Background of js c # code calls the front desk page could not be copied

Time:11-06

My demand is js calls to c # code, and then the c # code execution result is returned to the js code, can receive the js code. Then copy the value returned.

There are two solutions.
The first is the following js code
The function testClick () {
Var strHello="& lt; % %=test () & gt; ";
Alert (strHello);
}
Test () function returns a value is a string, if perform quick ok, js code oh strHello variables can receive the return value of c # code.
If the test () function inside the complex, the execution of the slow, strHello is receiving less than the value of the.

The second solution is
Js code to directly invoke the test (), but not received value, and then the test is in () function like this:

Public void test ()
{
ScriptManager. RegisterStartupScript (enclosing form1, enclosing Page. GetType (), ""," copyCallback (" hello ");" , true);
}

Js function like this:
 
The function copyCallback (text) {
Var input=document. The createElement method (" input ");
Input. SetAttribute (" id ", "copyInput");
Input. SetAttribute (' value, the text).
Document. The getElementsByTagName (' body ') [0]. The appendChild (input);
Document. The getElementById (" copyInput "). The select ();
If (document. ExecCommand (' copy ')) {
Document. The getElementById (" copyInput "). The remove ();
}
The else {
Document. The getElementById (" copyInput "). The remove ();
}
}


C # test () function calls the js pass parameters hello to the copyCallback copyCallback function, and then copy the code, but failure is copied.
Direct call front desk page copyCallback function can be copied, but the background copy failed calls to this function.

The two solutions, can solve any possible requirements, excuse me what method?
  • Related