使用ImageButton打开URL

本文关键字:URL 打开 ImageButton 使用 | 更新日期: 2023-09-27 18:24:55

使用asp.net | C#

我希望我的ImageButton在我点击它时打开一个URL。我终于加载了我的图像,它点击了,但当我点击它的时候,什么都没有发生。这是我迄今为止的代码:

aspx页面

   <asp:ImageButton ID="Button1" runat="server" ImageUrl="~/images/button.gif" 
    onclick="Open_Click"></asp:ImageButton>

aspx.cs页面

    protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        try
        {
            System.Diagnostics.Process.Start("http://www.website.com");
        }
        catch { }
    }

使用ImageButton打开URL

您想要进行重定向,而不是启动进程。试试这个:

protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
    try
    {
        Response.Redirect("http://www.website.com");
    }
    catch { }
}

此外,您只需在控件上设置PostBackUrl属性,就不需要服务器端事件。

您可以在客户端上完成:

这将在另一个窗口中打开:

<asp:ImageButton OnClientClick="window.open('/xxx/xxx.aspx');

或者这将在同一窗口中打开,javascript需要返回false,这样服务器代码就不会运行:

    <script>
        function ReDirect() {
            location.href = '/xxx/xxx.aspx';
            return false;
        }
    </script>
asp:ImageButton OnClientClick="javascript:return(ReDirect());" />