前些日子做项目试着实现了这个功能,有很多都是从网上搜来的资料,所以此程序中会看到有些与网上雷同的代码
一,代码如下:
<script type="text/javascript" language="javascript" >
//右键菜单
var x, y;
var mark1,mark2;
var j;
var idd;
function popUp(obj,itemmenu) //obj为响应右键的控件,itemmenu为右健菜单的id
{
idd=obj;
var obj = document.getElementById(idd);
if (event.button == 0 || event.button==2) //button==0表示在遨游浏览器中右键的判断,button==2表示在IE中右键的判断
{
var count=0;
for(i=0;i<obj.length;i++) //判断listbox中有无选中项
{
if(obj.options[i].selected==true)
{
count++;
break;
}
}
if(count!=0)
mark1=1; //count!=0说明有选中项,将选中项标志mark1设为1
else
mark1=0;
var str=window.clipboardData.getData("Text"); //取剪贴板中数据
if(str=='')
mark2=0; //如果为空,将非空标志设为0
else
mark2=1;
///////////四种情况下的右键菜单显示//////////
if(mark1==0&&mark2==0)
showMenu('0',itemmenu);
else if(mark1==0&&mark2==1)
showMenu('1',itemmenu)
else if(mark1==1&&mark2==0)
showMenu('2',itemmenu)
else
showMenu('3',itemmenu)
}
}
//根据传入的id显示右键菜单
function showMenu(id,itemmenu)
{
if(id=="0")
{
popMenu(itemmenu,50,"01010"); //1代表对应的行显示,0代表不显示
}
else if(id=="1")
{
popMenu(itemmenu,50,"01100");
}
else if(id=="2")
{
popMenu(itemmenu,50,"10010");
}
else
{
popMenu(itemmenu,50,"10100");
}
event.returnValue=false;
event.cancelBubble=true;
return false;
}
/**
*显示弹出菜单
*menuDiv:右键菜单的内容
*width:行显示的宽度
*rowControlString:行控制字符串,0表示不显示,1表示显示,如“101”,则表示第1、3行显示,第2行不显示
*/
function popMenu(itemmenu,width,rowControlString)
{
//创建弹出菜单
var pop=window.createPopup();
//设置弹出菜单的内容
pop.document.body.innerHTML=document.getElementById (itemmenu).innerHTML;
var rowObjs=pop.document.body.all[0].rows;
//获得弹出菜单的行数
var rowCount=rowObjs.length;
//循环设置每行的属性
for(var i=0;i<rowObjs.length;i++)
{
//如果设置该行不显示,则行数减一
var hide=rowControlString.charAt(i)!='1';
if(hide)
{
rowCount--;
}
//设置是否显示该行
rowObjs[i].style.display=(hide)?"none":"";
}
var mark3,mark4;
mark3=mark4=0;
///////复制键(有选项被选中时)///////////////
//设置鼠标滑入该行时的效果
rowObjs[0].cells[0].onmouseover=function()
{
this.style.background="#818181";
this.style.color="white";
mark3++;
}
//设置鼠标滑出该行时的效果
rowObjs[0].cells[0].onmouseout=function()
{
this.style.background="#cccccc";
this.style.color="black";
}
/////////////粘贴键(正常时)/////////////////
//设置鼠标滑入该行时的效果
rowObjs[2].cells[0].onmouseover=function()
{
this.style.background="#818181";
this.style.color="white";
mark4++;
}
//设置鼠标滑出该行时的效果
rowObjs[2].cells[0].onmouseout=function()
{
this.style.background="#cccccc";
this.style.color="black";
}
//屏蔽菜单的菜单
pop.document.oncontextmenu=function()
{
return false;
}
//选择右键菜单的一项后,菜单隐藏
pop.document.onclick=function()
{
if(mark3!=0||mark4!=0) ///////情况正常时菜单消失///////
pop.hide();
}
//显示菜单
pop.show(event.clientX-1,event.clientY,width,rowCount*25,document.body); //显示
return true;
}
function copy() //复制操作
{
if(mark1==1)
{
var str="";
var obj=document.getElementById(idd);
var addOption=document.createElement("option");
for(var i=0;i<obj.length;i++)
{
if(obj.options[i].selected==true)
{
if(obj.options[i].text.indexOf("|")!=-1)
{
var strsub=obj.options[i].text.split("|");
for(var j=0;j<strsub.length;j++)
{
str +=strsub[j]+"/t";
}
}
else
{
str +=obj.options[i].text;
}
str +="/r/n";
}
}
window.clipboardData.setData('text',str); //放入剪贴板
}
}
function paste(para1,para2,button) //粘贴操作
{
if(mark2==1)
{
var str=window.clipboardData.getData("Text"); //获取剪贴板中数据
var obj=document.getElementById(idd);
j=obj.length; //listbox中数据项个数
var ObjHid = document.getElementById(para1);
var ObjHidtext=document.getElementById(para2);
ObjHidtext.value=idd;
ObjHid.value = "";
var substr;
var i=0;
var strl=str.split("/n");
for(i=0;i<strl.length;i++)
{
substr=strl[i];
if(substr.length>1 ) //判断字符不为空
{
ObjHid.value += substr +",";
j++;
}
}
}
document.getElementById(button).click(); //控制Button的click,即等价于点击Button
}
</script>
<!-- 这里用来定义需要显示的右键菜单 -->
<div id="itemmenu1" style="display:none">
<table border="1" width="100%" height="100%" bgcolor="#cccccc" style="border:thin" cellspacing="0">
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.copy()">
<font size=2>
复制
</font>
</td>
</tr>
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.copy()">
<font color="white" size=2>
复制
</font>
</td>
</tr>
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.paste('unithidvalue1','unithidvalue2','ButtonPaste');">
<font size=2>
粘贴
</font>
</td>
</tr>
<tr>
<td style="cursor:default;border:outset 1;" align="center" onclick="parent.paste('unithidvalue1','unithidvalue2','ButtonPaste');">
<font color="white" size=2>
粘贴
</font>
</td>
</tr>
</table>
</div>
<!-- 右键菜单结束-->
服务器端:
protected void Page_Load(object sender, EventArgs e)
{
…………………….
……………………
//列表框右键菜单
listunitsource.Attributes.Add("onmousedown","popUp('listunitsource','itemmenu1');");
listunittarget.Attributes.Add("onmousedown", "popUp('listunittarget','itemmenu1');");
// listunitsource 和listunittarget为两个listbox
……………………
……………………
}
//右键粘贴
protected void ButtonPaste_Click(object sender, EventArgs e)
{
string strRight =unithidvalue1.Value; //隐藏text控件
string str = unithidvalue2.Value;
string[] arrRight = strRight.Split(new Char[] { ',' });
for (int i = 0; i < arrRight.Length; i++)
{
if (arrRight[i] != "")
{
ListItem x = new ListItem();
x.Text = arrRight[i];
x.Value = arrRight[i];
if (str == "listunitsource") listunitsource.Items.Add(x);
else if (str == "listunittarget") listunittarget.Items.Add(x);
}
}
}
二,要点和问题
(1)鼠标右键的响应:在listbox上实现鼠标点击事件listunitsource.Attributes.Add("onmousedown","popUp('listunitsource','itemmenu1');");
在listbox上添加add(),"onmousedown" 代表鼠标点击事件,popUp()也就是该事件所触发的客户端函数。客户端函数通过if (event.button == 0 || event.button==2) //button==0表示在遨游浏览器中右键的判断,button==2表示在IE中右键的判断 来判断鼠标事件,event.button 代表鼠标点击事件。
(2)直接在客户端将数据添加到listbox 在页面刷新后数据就会消失,因为未将数据写入服务器端,因此在粘贴功能函数中用到了document.getElementById(button).click(); //控制Button的click,即等价于点击Button
通过添加一个长宽均为0的button键(注意:button键的visable不能设为false,这样会使buttin键失去功能),通过button_click事件来实现在服务器端写入数据。
(3)客户端与服务器端传递数据的方法:在这里用的是添加隐藏text(此处勇HtmlInputHidden)控件来实现传递,在客户端通过将字符串加入控件 (ObjHid.value += substr +",";),中间用"," 隔开字符串,在服务器端获得控件的value,再通过Split函数来进行划分(string[] arrRight = strRight.Split(new Char[] { ',' });)获得每个字符串。
(4)str +=strsub[j]+"/t"中"/t"的用处:若不加,则粘贴到excel文档中时同一行的所有文字都会挤在同一格内,无论字符串之间有多少空格,比如”abc def hij”,若在字符串之间加上"/t"则在粘贴到excel中时字符串会分别占一格。
本文介绍了如何在.NET环境下实现右键菜单的复制和粘贴功能,通过JavaScript实现右键菜单的显示与操作,包括复制已选选项到剪贴板,以及从剪贴板粘贴数据。代码示例展示了如何处理不同情况下的右键菜单显示,并提供了复制和粘贴的函数实现。
584

被折叠的 条评论
为什么被折叠?



