在ASP.Net中,如何在新窗口中打开链接?

时间:2022-02-24 21:16:27

I have a button which redirects the user to another page. Instead, I would like this button to open a new window that points to this location. Can anyone please help me to do this?

我有一个按钮,可以将用户重定向到另一个页面。相反,我希望这个按钮打开一个指向这个位置的新窗口。任何人都可以帮我这样做吗?

aspx:

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClick="img_url_Click"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 />

cs:

protected void img_url_Click(object sender, ImageClickEventArgs e)
{
    ImageButton img = sender as ImageButton;
    Response.Redirect(img.CommandName.ToString());
}

3 个解决方案

#1


5  

Just use a standard HTML anchor instead, containing the image.

只需使用包含图像的标准HTML锚点即可。

<a href='<%# Eval("url") %>' target="_blank">
   <asp:Image RunAt="server"
              ID="detailsImage" 
              ImageUrl="~/images/products_details.png" />
</a>

#2


1  

You can add OnClientClick="formname.target='_blank'" to you ASP.net Controller.

您可以向您的ASP.net控制器添加OnClientClick =“formname.target ='_ blank'”。

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClick="img_url_Click"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 OnClientClick="formname.target='_blank'"
                 />

Your codebehind redirect will occur on your new page.

您的代码隐藏重定向将出现在您的新页面上。

#3


0  

Instead of onclick you could use onclientclick and pass the javascript window.open

您可以使用onclientclick并传递javascript window.open,而不是onclick

something like:

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClientClick="window.open('<%#Eval("url") %>')"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 />

#1


5  

Just use a standard HTML anchor instead, containing the image.

只需使用包含图像的标准HTML锚点即可。

<a href='<%# Eval("url") %>' target="_blank">
   <asp:Image RunAt="server"
              ID="detailsImage" 
              ImageUrl="~/images/products_details.png" />
</a>

#2


1  

You can add OnClientClick="formname.target='_blank'" to you ASP.net Controller.

您可以向您的ASP.net控制器添加OnClientClick =“formname.target ='_ blank'”。

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClick="img_url_Click"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 OnClientClick="formname.target='_blank'"
                 />

Your codebehind redirect will occur on your new page.

您的代码隐藏重定向将出现在您的新页面上。

#3


0  

Instead of onclick you could use onclientclick and pass the javascript window.open

您可以使用onclientclick并传递javascript window.open,而不是onclick

something like:

<asp:ImageButton ID="img_url"
                 CommandName='<%#Eval("url") %>'
                 OnClientClick="window.open('<%#Eval("url") %>')"
                 runat="server"
                 ImageUrl="~/images/products_details.png"
                 />