简体   繁体   English

响应。编写一个JavaScript代码,但不起作用

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

I used Response.Write in code behind to run javascript in client like that,it fired in a RowCommand event of gridview: 我在后面的代码中使用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);
}

I wrote a same code in client, it run okay, but in code behind, it doesn't work. 我在客户端编写了相同的代码,它可以正常运行,但是在后面的代码中,它不起作用。

编写脚本时,没有将query用引号引起来。

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

instead of response just use .RegisterStartupScript 而不是响应,只需使用.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM