简体   繁体   中英

how to open new window in new tab with dynamic url

i have a page with drop down list and i have to open a new window with selected iteam's edit form

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.EntityGrid.Columns.Clear();
    EntityName.Text = DropDownList1.SelectedItem.Text;
    newEntity.Visible = true;
    newEntity.Text = DropDownList1.SelectedItem.Text;
    ...
}

the following works

protected void newEntity_Click(object sender, EventArgs e)
{
    var entity = newEntity.Text;
    Response.Redirect(entity + "Edit.aspx");
    ...
}

but how can i open in separate tab not new window.

Open a separate window is a client feature, so you need to "inject" the javascript that tells the browser to do this.

Response.Write(
     string.Format(
        "<script>window.open('{0}','_blank');</script>", entity + "Edit.aspx"));

The parameter _blank tells the windows.open function to open a new window

You need to direct the browser to open a new window - this cannot be done server side, so you have to do so in client side.

One option is, instead of Response.Redirect , use Response.Write to output JavaScript to open a new window (and redirect the current one).

Another option is to use a target="_blank" attribute on a link that will open a new window.

You can forget about using JavaScript because the browser controls whether or not it opens in a new tab. Your best option is to do something like the following instead:

<form action="http://www.yoursite.com/dosomething" method="get" target="_blank">
    <input name="dynamicParam1" type="text"/>
    <input name="dynamicParam2" type="text" />
    <input type="submit" value="submit" />
</form>

This will always open in a new tab regardless of which browser a client uses due to the target attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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