JS调用后台事件?

fkueyygyispw 2010-02-25 11:22:14
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApp.positionManagement.WebForm2" %>

后台:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApp.positionManagement
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bindhtml();
}
public void bindhtml()
{
Response.Write("<html>");
Response.Write("<head>");
Response.Write("<title>测试</title>");
Response.Write("<script type=\"text/javascript\">");
Response.Write("function cheshi(){");
Response.Write("alert(\"进入1\");");
Response.Write("document.form1.action=\"Button1_Click\";");
Response.Write("document.form1.submit();");
Response.Write("alert(\"进入2\");");
Response.Write("}");
Response.Write("</script>");
Response.Write("</head>");
Response.Write("<body>");
string path = HttpContext.Current.Server.MapPath("~/positionManagement/WebForm2.aspx");
Response.Write(path);
Response.Write("<form name=\"form1\" action=\"" + path + "\" id=\"form1\">");
Response.Write("<div>");
Response.Write("<input id=\"btnAdd\" type=\"button\" value=\"确定\" onclick=\"return cheshi()\" />");
Response.Write("</div>");
Response.Write("<form>");
Response.Write("<from>");
Response.Write("</body>");
Response.Write("</html>");
}
public void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('测试成功!');</script>");
}
}
}
老是报个找不到路径?怎么解决呢?
...全文
789 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fkueyygyispw 2010-02-26
  • 打赏
  • 举报
回复
6楼兄弟的方法可以用,但是加载的时候Load里面的事件就会执得一次,除非有限制条件,但你说的用Ajax,我用AjaxPro的时候就出现了问题,不晓得是怎么回事?
Jelindu 2010-02-26
  • 打赏
  • 举报
回复
客户端调用直接调用服务端的事件,应该用ajax比较合理
getian 2010-02-26
  • 打赏
  • 举报
回复
这个建议用Ajax吧,不然肯定会回发。

浏览器是不懂你的“document.form1.action=\"Button1_Click\";"); ”是什么意思的。

改成
Response.Write("document.form1.action=~/positionManagement/WebForm2.aspx");
以后,在Load里面处理请求,也就是写你的事件代码,可以起到执行的作用,但是页面会刷新。

所以,还是建议用Ajax。

hudelan 2010-02-26
  • 打赏
  • 举报
回复
string path = HttpContext.Current.Server.MapPath("~/positionManagement/WebForm2.aspx");
必须有当前路径文件!
casear_huang 2010-02-26
  • 打赏
  • 举报
回复
不知道你为什么这么写,但是,你一定要这么写的话。
Response.Write("document.form1.action=\"Button1_Click\";");
要改成
Response.Write("document.form1.action=~/positionManagement/WebForm2.aspx");
Dream_Hunter_ 2010-02-25
  • 打赏
  • 举报
回复
楼主干嘛把html都在一个方法里输出?

网上直接复制的

前台JS调用后台C#方法
+++++++++++++++++第一种情况:
1.后台方法:
protected string CsharpVoid(string strCC)
{
return strCC;
}
2.javascript 调用
<script language="javascript">
var s = "<%=CsharpVoid("www.esoutong.com")%>";
document.write(s);
</script>


+++++++++++++++++第二种情况:
1.后代码:
protected void CsharpVoid()
{
string strCC = "www.esoutong.com";
Response.Write(strCC);
}
2.调用方法:CsharpVoid()
<script language="javascript">
document.write("<%CsharpVoid();%>");
</script>
「已注销」 2010-02-25
  • 打赏
  • 举报
回复
dingdingdingding
wuyq11 2010-02-25
  • 打赏
  • 举报
回复
想实现什么功能
在页面调用document.getElementById("").Click();

62,243

社区成员

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

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

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

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