响应.写一个javascript代码,但它不工作

Response.Write a javascript code but it doesn't work

本文关键字:代码 工作 javascript 一个 响应      更新时间:2023-09-26

我使用了Response。编写代码在客户端运行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);
}

我在客户端写了同样的代码,它运行正常,但在后台代码,它不工作

您在编写脚本时没有将query用引号括起来

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

使用。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);