比如在前台写了一个js方法
function test(name)
{
   alert(name)
}
如何在后台实现调用这个方法,并传参
有办法response.write(string str)来实现吗,具体怎么实现?

解决方案 »

  1.   

    .net 么
    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "test('name')",true);
      

  2.   

    str = "window.onload=function(){test('xxx')}"
    response.write str
      

  3.   

    this.Page.ClientScript.RegisterStartupScript(this.GetType(),"", "test("+name+");");
      

  4.   

    我把上面的代码加到一个按钮上单击事件里,点击只在是页面上输出了字符串,但是没有弹出alert对话框呢
      

  5.   

    你看看生成的源文件:
    生成的脚本是不是在<html></html>外面了,破坏了文档结构了。
    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "test('name')",true);
    这样不行么
      

  6.   


    protected void Button1_Click(object sender, EventArgs e)
            {
                string str = "window.onload=function(){test('xxx')}";
                Response.Write(str);
            }
      

  7.   

    是.net啊
    string str = "<script type='text/javascript'>window.onload=function(){test('xxx')}</script>";
      Response.Write(str);
    这样写如7楼所说会破坏文档结构,在顶层输出js
    你可以按他的做法来
    或者弄个label服务器控件什么的,把他的text属怀设置为这段js字符
      

  8.   

    7楼的方法可以!
    那如果我现在想在B页面点击一个按据来调用A页面的js方法,有没办法呢?
      

  9.   

    首先对之前我的回答表示歉意,之前的回答不正确。
    经过自己测试如下:
    1.aspx.cs文件的内容为:
            protected void Page_Load(object sender, EventArgs e)
            {
                this.Page.ClientScript.RegisterClientScriptInclude("test", "JScript1.js");
                string name = "tong";
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "test", "test('"+name+"');",true);
            }
    2.JScript1.js文件的内容为:
    function test(name)
    {
        alert(name);
    }
    3.JScript.js文件和aspx文件在同一目录下。