2013年11月14日 星期四

HyperLink按右鍵執行Script

HyperLink按右鍵執行Script
















----------------------------------------------------------------------------------------------------
.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tsHyperLink.aspx.cs" Inherits="Test_tsHyperLink" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">

        function deleteFile(arg1) {
            if (confirm("Are you sure want to delete?")) {
                alert(arg1); //出現警示視窗
                window.open("/deletefile.aspx?filename=" + arg1); //開另一個新視窗
                __doPostBack('LinkButton1', '') //執行一個onClick / onChange Event //必須在<$@Page />加入EnableEventValidation="false"
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/default.aspx" >HyperLink按右鍵執行Script</asp:HyperLink><br />
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" Visible="false">LinkButton</asp:LinkButton><br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

----------------------------------------------------------------------------------------------------
.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test_tsHyperLink : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HyperLink hpl = (HyperLink)this.form1.FindControl("HyperLink1");
        hpl.Attributes["oncontextmenu"] = "deleteFile('test.xls')";

        //關閉form1內的右鍵功能,避免干擾
        this.form1.Attributes.Add("oncontextmenu", "return false");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        // Do to...
        Label1.Text = "test";
    }
}

沒有留言:

張貼留言