0

我在表单 id="form1" runat="server" 标记之后在我的 aspx 页面中有这个:(必须去掉 > 和 <)

    <script type="text/javascript">
        function openLocationForm(locationId)
        {
            window.open('FlagLocationViewForm.aspx?id=' + locationId), '_blank');
        }
    </script>

FlagLocationViewForm.aspx 页面确实存在并且需要传递给它的参数。如果我只是让页面加载 Response.Redirect("FlagLocationViewForm.aspx"); 它加载,但不在新标签中。

我在 .cs 文件中有这个,该文件与具有上述脚本的这个 .aspx 页面相关联。

protected void GridViewCustomerList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    GridViewSelectEventArgs args = (GridViewSelectEventArgs)e;

    GridViewRow row = GridViewCustomerList.Rows[args.NewSelectedIndex];
    
    int customerId = Convert.ToInt32(row.Cells[6].Text);

    string scriptToRun = "openLocationForm('" + customerId.ToString() + "');";
    Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), scriptToRun, true);

}

该页面导致 GridViewCustomerList_SelectedIndexChanging 触发 - 一切正常。但是 RegisterStartupScript 没有做任何事情。如果我将以下内容替换为 Page.ClientScript.RegisterStartupScript 行:

Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Hello');", true);

我收到显示“你好”的警报。

谁能告诉我我做错了什么?

4

1 回答 1

0

我猜如果你检查浏览器的开发工具控制台,你会发现一条关于openLocationForm未定义的消息。

我认为这里可能发生的是该openLocationForm功能尚不存在。如果您检查源代码,您很可能会看到启动脚本出现函数定义之前。

将您的函数定义移到更高的位置(例如到头部),它应该可以工作。

于 2021-06-24T22:32:38.060 回答