Response.Write一个JavaScript代码,但它不起作用

时间:2013-06-19 02:25:51

标签: c# javascript asp.net response.write

我在后面的代码中使用Response.Write在客户端中运行javascript,它在gridview的RowCommand事件中被触发:

protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
    var index = int.Parse(e.CommandArgument.ToString());
    var val = gvwCus.DataKeys[index][0].ToString();
    PopupWindow("../Main/Detail.aspx?idc=" + val,900.ToString(),600.ToString());
}
private void PopupWindow(string query, string width, string height)
{
    var re = "<script language=\"javascript\">var left=(screen.width/2)-(" + width +"/2); var top=(screen.height/2)-(" + height + "/2); window.open(" + query +",'PopUp','toolbar=no, location=0, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=" + width + ", height=" + height + ",top=' + top + ',left=' + left);</script>";
    Response.Write(re);
}

我在客户端编写了相同的代码,运行正常,但在代码背后,它不起作用。

2 个答案:

答案 0 :(得分:1)

在编写脚本时,您没有在引号中包含query

... window.open('" + query +"','PopUp', ...

答案 1 :(得分:0)

而不是响应只需使用.RegisterStartupScript

    string script="var left=(screen.width/2)-(" + width +"/2); var top=(screen.height/2)-(" +
 height + "/2); window.open(" + query +",'PopUp','toolbar=no, location=0, directories=no,
 status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=" + width + ",
 height=" + height + ",top=' + top + ',left=' + left);"

ScriptManager.RegisterStartupScript(this, this.GetType(), "myalert", "script", true);