js调用后台的C#带参数的函数

hyclq 2010-03-24 10:50:34
我想要的效果不仅仅是js调用后台的函数,而且希望将前台定义的一个变量作为参数,调用后台的c#程序,如下:
前台:
function pageselectCallback(page_index, jq)
{
var index = page_index.toString();
"<%=SetVideoInfor(index)%>";
return false;
}

后台:
protected string SetVideoInfor(string index)
{
if (Session[currentPage].ToString() != index)
{
Session[currentPage] = Convert.ToInt32(index);
}
}
这样调用不行:编译器错误信息: CS0103: 当前上下文中不存在名称“index”

然后我在后台定义:public string index = "";
还是不行:编译器错误信息: CS1502: 与“System.IO.TextWriter.Write(char)”最匹配的重载方法具有一些无效参数
------------------------------------------------------------------------------------------------------------
这个问题弄了我好几天,请问大家有什么对策不??
...全文
799 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenlanglin 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
protected string A(string str)
{
str = ""+str;
return str;
}
function Fuc()
{
var v = "";
var s = '<%=A("'+v+'") %>';
alert(s);
}

xmlHttp.onreadystatechange = handstateChane;
xmlHttp.……
[/Quote]



此法正解
yuanzhaofu01 2010-03-24
  • 打赏
  • 举报
回复
用台要用return的
coderpig0730 2010-03-24
  • 打赏
  • 举报
回复
ajaxMethod
ctmdxd 2010-03-24
  • 打赏
  • 举报
回复
我又想了一下,刚才的方法(3楼的)有问题,每次回传都会调用SetVideoInfo,应改为
protected void Page_Load(object sender, EventArg e)
{
string index=Request.Form["hdn"];
if(index.Trim().Length>0)
SetVideoInfo(index);
}
ctmdxd 2010-03-24
  • 打赏
  • 举报
回复
<%%>里只能访问服务器端的东西,index是客户端的,当然不能访问。
必须先把客户端的值传给服务器端,然后服务端才能访问到。可以用hidden标签实现

<input id='hdn' type='hidden' value=''/>

<script>
function pageselectCallback(page_index, jq)
{
hdn.value = page_index.toString(); //保存到hidden
this.form.submit(); //提交给服务端
}
</script>


在服务端是这样的:
protected void Page_Load(object sender, EventArg e)
{
string index=Request.Form["hdn"];
SetVideoInfo(index);
}
nosuchtracter 2010-03-24
  • 打赏
  • 举报
回复
然后我在后台定义:public string index = "";
还是不行:编译器错误信息: CS1502: 与“System.IO.TextWriter.Write(char)”最匹配的重载方法具有一些无效参数
你这个错误和你调用cs方法没关系
应该是你别的地方赋值,参数类型不对
nosuchtracter 2010-03-24
  • 打赏
  • 举报
回复
function pageselectCallback(page_index, jq)
{
var index = page_index.toString();
"<%=SetVideoInfor('index') %>";
return false;
}
这样写下看
要不就用ajaxPro吧
sxiaohui8709253 2010-03-24
  • 打赏
  • 举报
回复
SetVideoInfor 你这个方法需要的index参数 你前台传部过来啊
ailin84 2010-03-24
  • 打赏
  • 举报
回复
JS想在页面调用后台的C#函数 要么回传一次 要么 ajax
lugsbbs 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nosuchtracter 的回复:]
然后我在后台定义:public string index = "";
还是不行:编译器错误信息: CS1502: 与“System.IO.TextWriter.Write(char)”最匹配的重载方法具有一些无效参数
你这个错误和你调用cs方法没关系
应该是你别的地方赋值,参数类型不对
[/Quote]
同意
wu_baochun 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 c20051104 的回复:]
ajaxMethod
[/Quote]

这个很简单
vip__888 2010-03-24
  • 打赏
  • 举报
回复
google ajaxpro
wuyq11 2010-03-24
  • 打赏
  • 举报
回复
protected string A(string str)
{
str = ""+str;
return str;
}
function Fuc()
{
var v = "";
var s = '<%=A("'+v+'") %>';
alert(s);
}

xmlHttp.onreadystatechange = handstateChane;
xmlHttp.open("Get", "AjaxInfo.aspx?str=" + str, true);
xmlHttp.send(null);
function handstateChane() {

}

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧