I used Response.Write in code behind to run javascript in client like that,it fired in a RowCommand event of gridview:

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.

有帮助吗?

解决方案

You did not wrap query in quotes when composing the script.

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

其他提示

instead of response just use .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归因
不隶属于 StackOverflow
scroll top