GridView实现右键菜单

这个参考楼主可以看看。希望采纳

default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "','朝阳公园' union

select '关羽','男',28,'guanyu@sanguo.com','中关村' union

select '张飞','男',27,'zhangfei@sanguo.com','八里庄西里' union

select '赵云','男',22,'zhaoyun@sanguo.com','东长安街' union

select '貂蝉','女',20,'diaochan@sanguo.com','避暑山庄'";

DB.Execute(sql);

}

private void GetInfo()

{

int id = 0;

if (!int.TryParse(Request.QueryString["id"], out id))

{

Response.Write("error");

return;

}

string sql = "select * from test_users where _id=" + id.ToString();

DataTable dt = DB.GetTable(sql);

if (dt.Rows.Count == 0)

{

Response.Write("error");

return;

}

DataRow row = dt.Rows[0];

StringBuilder sb = new StringBuilder();

sb.Append("<table>");

sb.AppendFormat("<tr><td>姓名:</td><td>{0}</td></tr>", row["_name"]);

sb.AppendFormat("<tr><td>性别:</td><td>{0}</td></tr>", row["_sex"]);

sb.AppendFormat("<tr><td>年龄:</td><td>{0}</td></tr>", row["_age"]);

sb.AppendFormat("<tr><td>邮件:</td><td>{0}</td></tr>", row["_email"]);

sb.AppendFormat("<tr><td>地址:</td><td>{0}</td></tr>", row["_address"]);

sb.Append("</table>");

Response.Write(sb.ToString());

}

private void DeleteInfo()

{

int id = 0;

if (!int.TryParse(Request.QueryString["id"], out id))

{

Response.Write("error");

return;

}

string sql = "delete from test_users where _id=" + id.ToString();

DB.Execute(sql);

Response.Write("true");

}

private void EditInfo()

{

int id = 0;

if (!int.TryParse(Request.QueryString["id"], out id))

{

Response.Write("error");

return;

}

string sql = "update test_users set _name='{1}',_sex='{2}',_age={3} where _id={0}";

sql = String.Format(sql, id, Request["name"].Replace("'", ""), Request["sex"] == "男" ? "男" : "女", int.Parse(Request["age"]));

DB.Execute(sql);

Response.Write("true");

}

}

DB.cs

using System;

using System.Data;

using System.Data.SqlClient;

public class DB

{

public DB()

{

}

public static SqlConnection SqlConn

{

get { return new SqlConnection("server=(local);database=tempdb;uid=sa;pwd=;"); }

}

public static DataTable GetTable(string sql)

{

using (SqlConnection conn = SqlConn)

{

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

DataSet ds = new DataSet();

da.Fill(ds);

return ds.Tables[0];

}

}

public static int Execute(string sql)

{

using (SqlConnection conn = SqlConn)

{

SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();

return cmd.ExecuteNonQuery();

}

}

}

default.js

function el(id){

return document.getElementById(id);

}

window.onload = function(){

el("GridView1").oncontextmenu = function(ev){

showMenu(ev);

return false;

};

document.onmousedown = function(){if(!over)hideMenu();};

};

var over = false;

var menu = null;

function showMenu(ev){

ev = ev || window.event;

hideMenu();

var e = ev.srcElement || ev.target;

if(e.tagName=="TH") return;

var x = ev.pageX || ev.clientX + document.documentElement.scrollLeft;

var y = ev.pageY || ev.clientY + document.documentElement.scrollTop;

menu = document.createElement("DIV");

menu.className = "menu";

menu.style.left = x+4+"px";

menu.style.top = y+"px";

document.body.appendChild(menu);

var id = e.parentNode.getElementsByTagName("td")[0].innerHTML;

e.parentNode.id = "tr_"+id;

var s = "<a href='javascript:showInfo({id});'>查看详细信息</a>";

s += "<a href='javascript:editInfo({id});'>修改用户信息</a>";

s += "<a href='javascript:deleteInfo({id});'>删除该条记录</a>";

s = s.replace(/{id}/g, id);

menu.innerHTML = s;

menu.onmouseover = function(){over=true;};

menu.onmouseout = function(){over=false;};

}

function hideMenu(){

if(menu){

document.body.removeChild(menu);

menu = null;

}

}

function showInfo(id){

hideMenu();

shideBody();

el("tit").innerHTML = "查看用户的详细信息";

el("bod").innerHTML = "<img src='wait.gif' alt='loading...' />";

el("bot").innerHTML = "<input type='button' value='关闭' onclick='cancelShide()' />";

var req = getReq();

req.open("GET", "default.aspx?getinfo=1&id="+id, true);

req.onreadystatechange = function(){

if(req.readyState==4){

el("bod").innerHTML = req.responseText;

}

};

req.send(null);

}

function editInfo(id){

hideMenu();

shideBody();

el("tit").innerHTML = "编辑用户信息";

el("bod").innerHTML = "<img src='wait.gif' alt='loading...' />";

el("bot").innerHTML = "<input type='button' value='确定' onclick='doEdit("+id+")' /> <input type='button' value='取消' onclick='cancelShide()' />";

var tds = el("tr_"+id).getElementsByTagName("td");

var boy = tds[2].innerHTML == "男";

var s = "<table>";

s += "<tr><td>姓名</td><td><input type='text' value='"+ tds[1].innerHTML +"' /></td></tr>";

s += "<tr><td>性别</td><td><input type='radio' name='rad_sex'"+ (boy?" checked":"") +" />男 <input type='radio' name='rad_sex'"+ (!boy?" checked":"") +" />女</td></tr>";

s += "<tr><td>年龄</td><td><input type='text' value='"+ tds[3].innerHTML +"' /></td></tr>";

s += "</table>";

el("bod").innerHTML = s;

}

function deleteInfo(id){

hideMenu();

if(!confirm("确定要删除该记录吗?"))return;

removeRow(el("tr_"+id));

var req = getReq();

req.open("GET", "default.aspx?delete=1&id="+id, true);

req.onreadystatechange = function(){};

req.send(null);

}

function doEdit(id){

var puts = el("bod").getElementsByTagName("input");

if(isNaN(puts[3].value)){

puts[3].select();return;

}

var tds = el("tr_"+id).getElementsByTagName("td");

tds[1].innerHTML = puts[0].value;

tds[2].innerHTML = puts[1].checked?"男":"女";

tds[3].innerHTML = puts[3].value;

var data = "name="+ escape(tds[1].innerHTML);

data += "&sex="+ escape(tds[2].innerHTML);

data += "&age="+ escape(tds[3].innerHTML);

var req = getReq();

req.open("POST", "default.aspx?edit=1&id="+id, true);

req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

req.onreadystatechange = function(){};

req.send(data);

cancelShide();

}

var shield;

function shideBody()

{

shield = document.createElement("DIV");

shield.id = "shield";

shield.style.position = "absolute";

shield.style.left = "0px";

shield.style.top = "0px";

shield.style.width = "100%";

shield.style.height = document.documentElement.scrollHeight+"px";

shield.style.background = "#333333";

shield.style.textAlign = "center";

shield.style.zIndex = "10";

shield.style.filter = "alpha(opacity=0)";

shield.style.opacity = 0;

document.body.appendChild(shield);

this.setOpacity = function(obj,opacity){

if(opacity>=1)opacity=opacity/100;

try{ obj.style.opacity=opacity; }catch(e){}

try{

if(obj.filters.length>0 && obj.filters("alpha")){

obj.filters("alpha").opacity=opacity*150;

}else{

obj.style.filter="alpha(opacity=\""+(opacity*150)+"\")";

}

}catch(e){}

}

var c = 0;

this.doAlpha = function(){

if (++c > 20){clearInterval(ad);return 0;}

setOpacity(shield,c);

}

var ad = setInterval("doAlpha()",1);

el("divh").style.display = "";

el("divh").style.marginTop = -75+document.documentElement.scrollTop + "px";

}

function cancelShide()

{

if(el("divh"))

{

el("divh").style.display = "none";

}

if(shield)

{

document.body.removeChild(shield);

shield = null;

}

}

function getReq()

{

var oHttpReq = null;

if(window.ActiveXObject)

oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");

else if(window.createRequest)

oHttpReq = window.createRequest();

else

oHttpReq = new XMLHttpRequest();

return oHttpReq;

}

function removeRow(e)

{

e.style.backgroundColor = "#ff0000";

setTimeout(function(){e.style.backgroundColor = "#ffcccc";},100);

setTimeout(function(){e.style.backgroundColor = "MistyRose";},300);

setTimeout(function(){e.style.display = "none";},450);

}