实现了DropDownList下拉框多选的功能,摆脱了普通的DropDownList只能实现单选而不能实现多选的问题:
前台html代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sys_Permission.aspx.cs" Inherits="SysManage_Sys_Permission" %>
<%@ Register TagPrefix="hw" Namespace="UNLV.IAP.WebControls" Assembly="DropDownCheckList" %>
<!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>
<link rel="stylesheet" href="SysManage.CSS" type="text/css" />
</head>
<body bgcolor="#f3f3f3">
<form id="form1" runat="server">
<div>
<table align="center" cellpadding="0" cellspacing="0" width="80%" border="0">
<tr align="center">
<td>
<asp:Label ID="ViewValue" runat=server></asp:Label>
</td>
</tr>
<tr>
<td>
<table align="center" cellpadding="0" cellspacing="0" width="100%" id="table1" runat="server">
<tr align="center">
<td>
<input type="radio" id="radio1" name="radio1" runat="server" value="1" checked />查看
</td>
</tr>
<tr align="center">
<td>
<input type="radio" id="radio2" name="radio1" runat="server" value="2" />查询
</td>
</tr>
<tr align="center">
<td>
<input type="radio" id="radio3" name="radio1" runat="server" value="3" />添加
</td>
</tr>
<tr align="center">
<td>
<input type="radio" id="radio4" name="radio1" runat="server" value="4" />修改
</td>
</tr>
<tr align="center">
<td>
<input type="radio" id="radio5" name="radio1" runat="server" value="5" />删除
</td>
</tr>
<tr align="center">
<td>
</td>
</tr>
</table>
<table align=center cellpadding=0 cellspacing=0 width=100% id="table2" runat=server visible=false>
<tr align="center">
<td>
</td>
</tr>
<tr align="center">
<td>
</td>
</tr>
<tr>
<td>
<hw:DropDownCheckList id="DropDownCheckList1" runat="server" DataTextField="Name" RepeatColumns=2 DisplayTextWidth=300 DataValueField="ID" DropImageSrc="../Images/expand.gif" TextWhenNoneChecked="--请选择工程综合管理授权操作--" DisplayTextList=Labels ClientCodeLocation="../JS/DropDownCheckList.js"></hw:DropDownCheckList>
</td>
</tr>
<tr align="center">
<td>
</td>
</tr>
<tr align="center">
<td>
</td>
</tr>
</table>
</td>
</tr>
<tr align="center">
<td>
<input id="submit" name="submit" runat="server" size="100" value="授 权" type="submit"
onserverclick="submit_ServerClick" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台c#代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class SysManage_Sys_Permission : System.Web.UI.Page
{
protected FileManager DropMenu = new FileManager();
protected SysManage SysRoleGrant = new SysManage();
public string GetPagePermission = "";
int selectid = 1;
string rolecode = "";
string UserCode = "";
string OperationID = "";
string AllMenu2ID = "";
int pageid;
protected void Page_Load(object sender, EventArgs e)
{
try
{
//获取左边页面传来的(ID+角色信息)
GetPagePermission = Request["PageID"].ToString();
}
catch
{
}
String[] getppid = GetPagePermission.Split(',');
rolecode = getppid[1].ToString();
pageid = Convert.ToInt32(getppid[0].ToString());
UserCode = Session["MX_Role_Code"].ToString();
//UserCode = "Test1,Test2,";
//pageid = 140;
//根据登陆用户角色以及页面ID获取工程项目管理中Meun1,Menu2的操作权限
OperationID = SysRoleGrant.GetProGrantByRole(UserCode, pageid);
//根据角色ID获取该角色对应的所有Menu2ID
AllMenu2ID = SysRoleGrant.GetMenu2IDByRole(UserCode);
ViewValue.Text = AllMenu2ID;
if (!Page.IsPostBack)
{
if (pageid == 140)
{
table2.Visible = true;
table1.Visible = false;
DropPageMenuBind();
}
}
}
//绑定工程项目管理的权限
public void DropPageMenuBind()
{
DropDownCheckList1.DataSource = DropMenu.GetProGrantName("OperationGrant", "2");
DropDownCheckList1.DataBind();
DropDownCheckList1.DataValueField = "ID";
DropDownCheckList1.DataTextField = "Name";
}
protected void submit_ServerClick(object sender, EventArgs e)
{
if (pageid != 140)
{
if (radio1.Checked == true)
{
selectid = Convert.ToInt32(radio1.Value);
}
else if (radio2.Checked == true)
{
selectid = Convert.ToInt32(radio2.Value);
}
else if (radio3.Checked == true)
{
selectid = Convert.ToInt32(radio3.Value);
}
else if (radio4.Checked == true)
{
selectid = Convert.ToInt32(radio4.Value);
}
else if (radio5.Checked == true)
{
selectid = Convert.ToInt32(radio5.Value);
}
//pageid != 140的添加授权
SysRoleGrant.UpdataRoleGrant(rolecode, pageid, selectid);
Response.Write("<script language='javascript'>alert('授权成功!!');window.close();</script>");
}
else
{
string OperationID = this.DropDownCheckList1.SelectedValuesToString(",").ToString().Trim() + ",";
string[] OperArr = OperationID.Split(',');
//是否选择授权信息
if (OperArr[0] == "")
{
Response.Write("<script language='javascript'>alert('请选择授权权限!!');window.close();</script>");
}
else
{
//查找该角色是否已经授权
int k = SysRoleGrant.GetProGrant(rolecode, pageid);
//已经授权则先删除后添加授权信息
if (k != 0)
SysRoleGrant.DeleteProGrantByRoleID(rolecode);
for (int i = 0; i < OperArr.Length - 1; i++)
{
//pageid == 140的添加授权
SysRoleGrant.AddProGrant(rolecode, pageid,Convert.ToInt32(OperArr[i]));
Response.Write("<script language='javascript'>alert('授权成功!!');window.close();</script>");
}
}
}
}
}
js代码
/************************************************************************
* DropDownCheckList.js
*
* Client-side javascript to support the ASP.NET DropDownCheckList
* server control.
*
* written by Mike Ellison, 20-September-2005
*
************************************************************************
* Copyright (c) 2005 Mike Ellison. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions, the following disclaimer, and the following
* acknowledgements.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Acknowledgements:
* - The "shim" technique used to support Internet Explorer 6.x
* is courtesy Joe King of Coalesys, Inc.,
* http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
*
* - Javascript functions findPosX and findPosY for locating objects absolutely
* are courtesy Peter-Paul Koch, http://www.quirksmode.org
*
************************************************************************/
/************************************************************************
* Utility Functions (global to the page)
************************************************************************/
var DDCL_DROPDOWNMODE_INLINE = 0;
var DDCL_DROPDOWNMODE_ONTOP = 1;
var DDCL_DROPDOWNMODE_ONTOPWITHSHIM = 2;
var DDCL_DISPLAYTEXTLIST_LABELS = 0;
var DDCL_DISPLAYTEXTLIST_VALUES = 1;
ddcl_Objects = new Array();
function DDCL_GetObject(id)
{
for (var i=0; i<ddcl_Objects.length; i++)
{
// alert(id.substring(ddcl_Objects[i].length) + ", " + ddcl_Objects[i].id);
if (id.substring(0,ddcl_Objects[i].id.length) == ddcl_Objects[i].id)
{
// alert('gotit');
return ddcl_Objects[i];
}
}
return null;
}
// -----------------------------------------------------
function DDCL_findPosX(obj)
{
/***
Original script by Peter-Paul Koch, http://www.quirksmode.org
***/
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function DDCL_findPosY(obj)
{
/***
Original script by Peter-Paul Koch, http://www.quirksmode.org
***/
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}
// -----------------------------------------------------
function DDCL_HandleCheckboxClickById(id)
{
var obj = DDCL_GetObject(id);
obj.HandleCheckboxClick();
}
// called as the onclick event handler for a check box
function DDCL_HandleCheckboxClick(e)
{
if (!e) var e = window.event;
if (e) {
var elem = this;
if (elem) {
var obj = DDCL_GetObject(elem.id);
if (obj)
{
obj.HandleCheckboxClick();
obj.inCheckboxDiv = true;
}
}
}
}
// called as the onclick event handler for a display box
function DDCL_HandleDisplayBoxClick(e)
{
if (!e) var e = window.event;
if (e) {
var elem = this;
if (elem) {
var obj = DDCL_GetObject(elem.id);
if (obj)
{
obj.HandleDisplayBoxClick();
obj.inCheckboxDiv = true;
}
}
}
}
// called as the onclick event handler for a checkbox div
function DDCL_HandleCheckboxDivClick(e)
{
if (!e) var e = window.event;
if (e) {
var obj=DDCL_GetObject(this.id);
if (obj)
obj.inCheckboxDiv = true;
}
}
function DDCL_HandleDocumentClick(id)
{
var obj = DDCL_GetObject(id);
if (obj)
{
if (obj.inCheckboxDiv == true)
obj.inCheckboxDiv = false;
else
obj.CloseCheckList();
}
}
/************************************************************************
* Object definition
************************************************************************/
// constructor; takes a required id as a parameter; this will be the
// same as the ClientID of the ASP.NET control
function DDCL_DropDownCheckList(id, textWhenNone, separator, truncateString, dropDownMode, allowExpand, displayList)
{
// remember the id, displayMode, and text to display when no
// boxes are checked
this.id = id;
this.textWhenNone = textWhenNone;
this.separator = separator;
this.truncateString = truncateString;
this.dropDownMode = dropDownMode;
this.allowExpand = allowExpand;
this.displayList = displayList;
// get the elements representing the display box div and
// checklist div; these will be referenced by object methods
this.divCheckboxes = document.getElementById(id + "_checkboxes");
this.divBoundingBox = document.getElementById(id + "_boundingbox");
this.divText = document.getElementById(id + "_text");
this.divDisplayBox = document.getElementById(id + "_displaybox");
this.img = document.getElementById(id + "_img");
this.shim = document.getElementById(id + "_shim");
this.boundingBoxWidth = this.divBoundingBox.offsetWidth;
this.divCheckboxes.style.display = "none";
// wire click events for the display box div and the checkboxes div
this.divDisplayBox.onclick = DDCL_HandleDisplayBoxClick;
this.divCheckboxes.onclick = DDCL_HandleCheckboxDivClick;
// wire click events for the checkboxes
var e = this.divCheckboxes.getElementsByTagName("input");
for (var i=0; i<e.length; i++)
{
if (e[i].type == "checkbox")
{
e[i].onclick = DDCL_HandleCheckboxClick;
}
}
// if the browser supports bubbling events, install a default click
// handler for the document too, that will close the checkboxes div
// if there is a click outside it
if (document.attachEvent)
{
document.attachEvent('onclick'
, function() { eval("DDCL_HandleDocumentClick('" + id + "');") }
);
}
else if (document.addEventListener)
{
document.addEventListener('click'
, function() { eval("DDCL_HandleDocumentClick('" + id + "');") }
, false);
}
// initial display of checked items, or the textWhenNone string
this.DisplayCheckedItems();
// add this object to our reference array on the page so we can get it
// back later
ddcl_Objects.push(this);
}
// get a label given a checkbox element
function DDCL_DropDownCheckList_GetLabelForCheckbox(elem)
{
var e = this.divCheckboxes.getElementsByTagName("label");
for (var i=0; i<e.length; i++)
{
if (e[i].htmlFor == elem.id)
{
for (var j=0; j<e[i].childNodes.length; j++)
{
if (e[i].childNodes[j].nodeType == 3) //text type
{
return e[i].childNodes[j].nodeValue;
}
}
}
}
// still here? no <label> for this checkbox then
return null;
}
// refresh the checked items listing in the display box
function DDCL_DropDownCheckList_DisplayCheckedItems()
{
var sLabel = "";
var sCurrent = "";
var sFull = "";
var sBefore = "";
var sCompText = "";
var bEllipsisAdded = false;
// get all checkboxes in the checklist
var e = this.divCheckboxes.getElementsByTagName("input");
// clear the display text
this.divText.innerHTML = "";
// clear the title (tooltip) attribute
this.divDisplayBox.title = "";
// loop through all checkboxes in the checklist to see
// which ones are checked;
for (var i=0; i<e.length; i++)
{
if (e[i].type == "checkbox" && e[i].checked)
{
// if the checkbox is checked, get its associated label text
if (this.displayList == DDCL_DISPLAYTEXTLIST_LABELS)
// get the label for the checkbox
sLabel = this.GetLabelForCheckbox(e[i]);
else
// get the value for the checkbox
sLabel = e[i].value;
// add the list separator if necessary
if (sCurrent != "")
{
sCurrent += this.separator;
sFull += this.separator;
}
sFull += sLabel;
sCurrent += sLabel;
if (bEllipsisAdded == false)
{
// add this one to the text box, then test for the
// width against the display box
this.divText.innerHTML = "<nobr>" + sCurrent + "</nobr>";
if (this.divText.offsetWidth > this.boundingBoxWidth && this.allowExpand == false)
{
// too big; shrink by what we can and add the ellipsis (or other trunacte string)
while (this.divText.offsetWidth > this.boundingBoxWidth && sCurrent.length > 0)
{
sCurrent = sCurrent.substr(0, sCurrent.length - 1);
this.divText.innerHTML = "<nobr>" + sCurrent + this.truncateString + "</nobr>";
}
// and indicate the ellipsis (or other truncate text) has been added
bEllipsisAdded = true;
}
}
}
}
// finally, if there are no contents, display the textWhenNone message
if (this.divText.innerHTML == "")
{
if(this.textWhenNone == "")
this.divText.innerHTML = " ";
else
this.divText.innerHTML = this.textWhenNone;
}
// if we added the ellipsis, set the title attribute to the full string
// (which will display as a tooltip in most browsers)
if (bEllipsisAdded)
this.divDisplayBox.title = sFull;
else
this.divDisplayBox.title = "";
}
// handle a click in a participating checkbox
function DDCL_DropDownCheckList_HandleCheckboxClick()
{
// refresh the checked items display box
this.DisplayCheckedItems();
}
// handle a click in the display box
function DDCL_DropDownCheckList_HandleDisplayBoxClick()
{
// toggle the display of the checklist
this.ToggleCheckList();
}
// toggle the display of the checklist boxes
function DDCL_DropDownCheckList_ToggleCheckList()
{
if (this.divCheckboxes.style.display != "none")
this.CloseCheckList();
else
this.OpenCheckList();
}
// explicitly close the checklist
function DDCL_DropDownCheckList_CloseCheckList()
{
// hide the checkboxes div
this.divCheckboxes.style.display = "none";
// if we're using a shim, hide it as well
if (this.dropDownMode == DDCL_DROPDOWNMODE_ONTOPWITHSHIM)
this.shim.style.display = "none";
}
// explicitly open the checklist
function DDCL_DropDownCheckList_OpenCheckList()
{
// open the checkboxlist; first, position it below the displaybox
// determine the position based on the dropDownMode
if (this.dropDownMode == DDCL_DROPDOWNMODE_INLINE)
{
// inline mode; we're already setup as we need to be
this.divCheckboxes.style.display = "block";
}
else
{
// on top modes; position the box
this.divCheckboxes.style.left = DDCL_findPosX(this.divDisplayBox);
this.divCheckboxes.style.top = DDCL_findPosY(this.divDisplayBox) + this.divDisplayBox.offsetHeight;
this.divCheckboxes.style.display = "block";
// if we want the shim, apply that now
if (this.dropDownMode == DDCL_DROPDOWNMODE_ONTOPWITHSHIM)
{
this.shim.style.width = this.divCheckboxes.offsetWidth;
this.shim.style.height = this.divCheckboxes.offsetHeight;
this.shim.style.top = this.divCheckboxes.style.top;
this.shim.style.left = this.divCheckboxes.style.left;
this.shim.style.zIndex = this.divCheckboxes.style.zIndex - 1;
this.shim.style.display = "block";
}
}
}
// wire methods as prototypes
DDCL_DropDownCheckList.prototype.DisplayCheckedItems = DDCL_DropDownCheckList_DisplayCheckedItems;
DDCL_DropDownCheckList.prototype.GetLabelForCheckbox = DDCL_DropDownCheckList_GetLabelForCheckbox;
DDCL_DropDownCheckList.prototype.HandleCheckboxClick = DDCL_DropDownCheckList_HandleCheckboxClick;
DDCL_DropDownCheckList.prototype.HandleDisplayBoxClick = DDCL_DropDownCheckList_HandleDisplayBoxClick;
DDCL_DropDownCheckList.prototype.ToggleCheckList = DDCL_DropDownCheckList_ToggleCheckList;
DDCL_DropDownCheckList.prototype.CloseCheckList = DDCL_DropDownCheckList_CloseCheckList;
DDCL_DropDownCheckList.prototype.OpenCheckList = DDCL_DropDownCheckList_OpenCheckList;