前台:
<%@ 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>");
        } 
    }
}
老是报个找不到路径?怎么解决呢?

解决方案 »

  1.   

    想实现什么功能
    在页面调用document.getElementById("").Click();
      

  2.   

    楼主干嘛把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>
      

  3.   

    不知道你为什么这么写,但是,你一定要这么写的话。
    Response.Write("document.form1.action=\"Button1_Click\";"); 
    要改成
    Response.Write("document.form1.action=~/positionManagement/WebForm2.aspx"); 
      

  4.   

     string path = HttpContext.Current.Server.MapPath("~/positionManagement/WebForm2.aspx"); 
    必须有当前路径文件!
      

  5.   

    这个建议用Ajax吧,不然肯定会回发。浏览器是不懂你的“document.form1.action=\"Button1_Click\";"); ”是什么意思的。改成 
    Response.Write("document.form1.action=~/positionManagement/WebForm2.aspx"); 
    以后,在Load里面处理请求,也就是写你的事件代码,可以起到执行的作用,但是页面会刷新。所以,还是建议用Ajax。
      

  6.   

    客户端调用直接调用服务端的事件,应该用ajax比较合理
      

  7.   

    6楼兄弟的方法可以用,但是加载的时候Load里面的事件就会执得一次,除非有限制条件,但你说的用Ajax,我用AjaxPro的时候就出现了问题,不晓得是怎么回事?