Reputation:
I've been searching around on how to open a page in a new tab by clicking a button. Most codes written online are the same but when i try all of them, it doesn't work and no error is stated either.
this is my codes:
aspx.cs:
protected void btngen_Click(object sender, EventArgs e)
{
string queryString = "Barcode test.aspx";
string newWin = "window.open('" + queryString + "','_blank');";
ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}
aspx:
<div class ="form-group">
<label for="TxtBarcode" class="col-sm-2 control-label"> Barcode : </label>
<div class="col-sm-2">
<asp:TextBox ID="txtCode" CssClass="form-control" runat="server" />
</div>
<div class ="col-sm-2">
<asp:Button ID="btngen" runat="Server" CssClass="btn btn-success" Text="Generate barcode" OnClick="btngen_Click" />
</div>
</div>
Upvotes: 0
Views: 266
Reputation: 37
You have to use Linkbutton in redirect to any href or .aspx page
.aspx:
<asp:LinkButton ID="LinkButton1" href="index.aspx" target="_blank" runat="server">LinkButton</asp:LinkButton>
or
<asp:LinkButton ID="LinkButton1" href="http:\\www.google.com" target="_blank" runat="server">LinkButton</asp:LinkButton>
Upvotes: 0
Reputation: 18127
Change btnget to LinkButton
after that add attribute to this link button in the code behind:
btnget.Attributes.Add("target", "_blank");
Other possibility javascript
<asp:LinkButton ID="btngen" runat="Server" CssClass="btn btn-success" Text="Generate barcode" OnClick="btngen_Click" OnClientClick="return OpenNewTab();" />
//in the ASPX:
<script type="text/javascript">
function OpenNewTab() {
document.forms[0].target = '_blank';
}
</script>
Upvotes: 0