H_79
H_79

Reputation: 217

Open New Tab in ASP.NET web page

After clicking on ASP.NET button, it redirects to correct website but on the same tab, not in a new tab, which I need to do. After clicking the button twice, it redirects the website in a new tab. I don't know what's wrong with my code!

The ASP.NET button control looks like this:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../Images/neu.png" OnClick="New_btn_click" />

And the code that is run is this:

protected void New_btn_click(object sender, EventArgs e)
{
    ImageButton1.Attributes.Add("onclick", "window.open('new_model_epk.aspx');return false;");   
}

Upvotes: 1

Views: 6952

Answers (1)

woz
woz

Reputation: 10994

What you're describing is exactly what I would expect. The JavaScript isn't run until the second time because it hasn't been added until after you click the button the first time. Don't even use C#; just use this:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../Images/neu.png" OnClientClick="window.open('new_model_epk.aspx');return false;" />

Any JavaScript in the OnClientClick attribute will run right away when you click the element, so C# code is unnecessary.

Upvotes: 2

Related Questions