在asp页里面有Javascript函数,如何用C#脚本在按钮事件中调用这个函数并得到返回值?

解决方案 »

  1.   

    cs页的代码不能直接取到javascript函数的返回值
    把你的JS函数执行的结果保存到1个隐藏域中,然后在后台取隐藏域的值。<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %><!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 runat="server">
        <title>无标题页</title>
        <script language="javascript" type="text/javascript">
        function aa()
        {
            document.getElementById('Hidden1').value='aa';
            //return 'aa';
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Hidden1" name="Hidden1" value="bb" type="hidden" /></div>
            <asp:Button ID="btn" runat="server"  OnClientClick="aa()" OnClick="btn_Click" />
        </form>
    </body>
    </html>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class Default7 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void btn_Click(object sender, EventArgs e)
        {
            string str = Request["Hidden1"];
            if (str != null)
                Response.Write(str);
        }
    }
      

  2.   

    第二种方法,即用Ajax方法或 XHttpReqest回传得到<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
            <script language="javascript">
    function loaddays()
    {
    var ddlDay = document.getElementById("ddlDay");
    ddlDay.innerText = "";
    for(var i=0;i<ddlDay.length;i++)
    ddlDay.remove(i);

    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
    var year = document.getElementById("ddlYear").value;
    var month = document.getElementById("ddlMonth").value;
    xmlhttp.open("POST","demo_testyearmonthday.aspx?Year="+year+"&Month="+month,false);
    xmlhttp.send(null);

    var days = xmlhttp.responseText;
    for(var i=1;i<=days;i++)
    {
    var newoption = document.createElement("OPTION");
    newoption.text = i;
    newoption.value = i;
    ddlDay.options.add(newoption);
    }
    }
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    实例之年月日三级无刷新联动
    <asp:DropDownList ID="ddlYear" runat="server">
    </asp:DropDownList>
    <asp:DropDownList ID="ddlMonth" runat="server">
    </asp:DropDownList>
    <asp:DropDownList ID="ddlDay" runat="server">
    </asp:DropDownList>
        
        </div>
        </form>
    </body>
    </html>
     private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!IsPostBack)
            {
                if (Request["Year"] == null && Request["Month"] == null)
                {
                       response.write(Request["Year"].toString());
                  }
                else
                {
                    GetDays();
                }
            }
        }