目前有2个解决方案都有问题.
第一个是如下js代码
function testClick() {
var strHello = "<%=test()%>";
alert(strHello);
}
test()函数的的返回值是一个字符串,如果执行快没事,js代码啊的strHello 变量可以接收到C#代码返回的值.
如果test()函数里面比较复杂,执行的慢了,strHello 是接收不到值的.
第二个解决方案是
js代码直接调用了test(),但是不接收值,然后test()函数里是这样的:
public void test()
{
ScriptManager.RegisterStartupScript(this.form1, this.Page.GetType(), "", "copyCallback('hello');", true);
}
js函数是这样的:
function copyCallback(text) {
var input = document.createElement('input');
input.setAttribute('id', "copyInput");
input.setAttribute('value', text);
document.getElementsByTagName('body')[0].appendChild(input);
document.getElementById("copyInput").select();
if (document.execCommand('copy')) {
document.getElementById("copyInput").remove();
}
else {
document.getElementById("copyInput").remove();
}
}
C#的test()函数调用了js的copyCallback函数把参数hello传给了copyCallback,然后就是复制代码了,但是会复制失败.
前台页面直接调用copyCallback函数是可以复制的,但是后台调用这个函数就复制失败了.
这2个解决方案,能解决任意一个都可以实现的的需求,请问有什么办法吗?