I have this in my aspx page right after the form id="form1" runat="server" tag: (had to strip the > and <)
<script type="text/javascript">
function openLocationForm(locationId)
{
window.open('FlagLocationViewForm.aspx?id=' + locationId), '_blank');
}
</script>
The FlagLocationViewForm.aspx page does exist and expects a parameter passed to it. If I just cause the page to load with a Response.Redirect("FlagLocationViewForm.aspx"); it loads, but not in a new tab.
And I have this in the .cs file that is associated with this .aspx page that has the script mentioned above.
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);
}
The page is causing GridViewCustomerList_SelectedIndexChanging to fire - that all works. But the RegisterStartupScript does not do anything. If I substitute the following for the Page.ClientScript.RegisterStartupScript line:
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Hello');", true);
I get the alert that says 'Hello' displayed.
Can anybody tell me what I am doing wrong?
I am guessing if you inspect the Dev Tools console of your browser, you will find a message about
openLocationFormbeing undefined.I think what may be happening here is that the
openLocationFormfunction does not exist yet. If you inspect the source code, you will most likely see the startup script appear before the function definition.Move your function definition higher (e.g. to the head section) and it should work.