【问题标题】:Response.Write a javascript code but it doesn't workResponse.Write 一个 javascript 代码,但它不起作用
【发布时间】:2013-06-19 02:25:51
【问题描述】:

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

我在客户端写了一个相同的代码,运行正常,但是在后面的代码中,它不起作用。

【问题讨论】:

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


    【解决方案1】:

    编写脚本时,您没有将 query 括在引号中。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        相关资源
        最近更新 更多