1

单击 ASP.NET 按钮后,它重定向到正确的网站,但在同一个选项卡上,而不是在新选项卡中,我需要这样做。单击该按钮两次后,它会将网站重定向到新选项卡中。我不知道我的代码有什么问题!

ASP.NET 按钮控件如下所示:

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

运行的代码是这样的:

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

1 回答 1

2

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.

于 2013-05-02T15:07:06.333 回答