环境:VS2005 C#小弟试用了一个最简单方法:
页面放隐藏按钮,后台添加按钮点击函数,JS里document.all("Button1").click();但运行时弹出了拒绝访问的警告框。请问有什么办法可以实现JS调用后台C#函数?最好给出示例代码,谢谢!

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title></title> 
        <script language="javascript" type="text/javascript">
            function printTimeNow() {
                document.getElementById("Button2").click();
            }
        </script>   
    </head>
    <body>
        <form id="form1" runat="server">
        <div>        
            <input id="Button1" type="button" value="button"  onclick="printTimeNow()" />
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
        </div>
        </form></body>
    </html>
            protected void Button2_Click(object sender, EventArgs e)
            {
                Response.Write(DateTime.Now);
            }
      

  2.   

    document.getElementById("Button1").click();public string A()
               {
                  return "";
               }
    var a = "<%=A()%>";
      

  3.   

    感谢大家回贴帮助,我发现自己的问题比想像中更麻烦:我使用了一个网上下载的htmlEdit,我想在它包含的JS文件中调用后台C#函数,结果我杯具的发现:
    它和后台隔的实在太远了,别说执行后台函数了,就是我给一个控件赋值,后台都取不到。如果是在自己aspx页面的JS中,我相信大家方法包括我题头方法都是可行的。好在我各种绕,最终"巧秒"地避开了这个问题,虽然牺牲了部分程序性能,但尚在客户可接受范围之内。。唉,水平差,就学会“绕”了。。再次感谢大家回贴帮助,谢谢!