这个是在窗体一加载的时候就生成一个 checkBoxList .
function OnLoad_AllUser()
{
var res = Admin_usersRight.allRightValue(); //返回表中对应的角色说明出来
var roleCommentId;
var rightRole;
var comment;
var info = "";
var allComment = res.value.split("~");
roleCommentId = allComment[0].split(",");
rightRole = allComment[1].split(",");
comment = allComment[2].split(",");
for(var i = 0 ; i < roleCommentId.length - 1; i++)
{
info += '<tr><td><input id="CheckBoxList1_'+ rightRole[i] + '" type="checkbox" onclick="onclick_Value(' + rightRole[i] + ');" name="CheckBoxList1$' + rightRole[i] +'" /><label for="CheckBoxList1_' + rightRole[i] +'">' + comment[i] + '</label></td></tr>'
}
document.getElementById("allRight").innerHTML = '<table id="Table1" border="0">' + info + '<tr><td><input type="button" id="btnRightSet" onclick="rightSet(1)" value="确定" /> </td></tr></table>';
}
function OnLoad_AllUser()
{
var res = Admin_usersRight.allRightValue(); //返回表中对应的角色说明出来
var roleCommentId;
var rightRole;
var comment;
var info = "";
var allComment = res.value.split("~");
roleCommentId = allComment[0].split(",");
rightRole = allComment[1].split(",");
comment = allComment[2].split(",");
for(var i = 0 ; i < roleCommentId.length - 1; i++)
{
info += '<tr><td><input id="CheckBoxList1_'+ rightRole[i] + '" type="checkbox" onclick="onclick_Value(' + rightRole[i] + ');" name="CheckBoxList1$' + rightRole[i] +'" /><label for="CheckBoxList1_' + rightRole[i] +'">' + comment[i] + '</label></td></tr>'
}
document.getElementById("allRight").innerHTML = '<table id="Table1" border="0">' + info + '<tr><td><input type="button" id="btnRightSet" onclick="rightSet(1)" value="确定" /> </td></tr></table>';
}
下面这代码是对当选择某个checkBox时进行的操作
function onclick_Value( m ) //m是要设定的权限值
{
if(document.getElementById("CheckBoxList1_" + m).checked)
{
countNo += m + '~'; //当被选中时,就把他的m值记录下来
}
else //又去掉时先前选中的m值
{
if(countNo.length>0)
{
var no = countNo.split("~"); //对 countNo 进行分析
var max = no.length - 1; //取得最大值
for(var i = 0; i < max ; i++)
{
//如果用户去掉对钩时,那么这个地方就是把前先那个 m 值给掉,然后把后面的
//往前移. 如 countNo = "1,2,3,4,5,6,7,8," m = 4 ,现在就得把countNo 里的4去掉
if(no[i] == m)
{
for(var j = i ; j < max ; j ++)
{
no[j] = no[j+1]
}
}
}
countNo = "";
// 下面这里重新给 countNo 赋值,些时 countNo = "1,2,3,5,6,7,8,";
for(var i = 0 ; i < max -1 ; i++)
{
countNo += no[i] + '~';
}
}
else
{
}
}
}
{
if(document.getElementById("CheckBoxList1_" + m).checked)
{
countNo += m + '~'; //当被选中时,就把他的m值记录下来
}
else //又去掉时先前选中的m值
{
if(countNo.length>0)
{
var no = countNo.split("~"); //对 countNo 进行分析
var max = no.length - 1; //取得最大值
for(var i = 0; i < max ; i++)
{
//如果用户去掉对钩时,那么这个地方就是把前先那个 m 值给掉,然后把后面的
//往前移. 如 countNo = "1,2,3,4,5,6,7,8," m = 4 ,现在就得把countNo 里的4去掉
if(no[i] == m)
{
for(var j = i ; j < max ; j ++)
{
no[j] = no[j+1]
}
}
}
countNo = "";
// 下面这里重新给 countNo 赋值,些时 countNo = "1,2,3,5,6,7,8,";
for(var i = 0 ; i < max -1 ; i++)
{
countNo += no[i] + '~';
}
}
else
{
}
}
}
下面就是对刚才的操作开始执行了
function rightSet(status) //这个是
{
var username = document.getElementById("txtUsername").value;
var aa = Admin_usersRight.setUserRight(countNo,username,status,_doPostRightSetCallBack);
ShowUserRight();
}
{
var username = document.getElementById("txtUsername").value;
var aa = Admin_usersRight.setUserRight(countNo,username,status,_doPostRightSetCallBack);
ShowUserRight();
}
下面是我的cs里的代码
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 Admin_usersRight : adminBasePase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_usersRight));
}
}
/// <summary>
/// 添加用户权限
/// </summary>
/// <param name="userId">用户ID号</param>
/// <param name="rightValue">权限值</param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static int addUserRight(int userId,int rightValue)
{
int count = 0;
if (returnValue.userRightIsExist(userId, rightValue) == true)
{
}
else
{
if (addOperator.addUserRight(userId, rightValue) == true)
{
count = 1;
}
else
{
count = 2;
}
}
return count;
}
/// <summary>
/// 是否存在此权限
/// </summary>
/// <param name="rightId">权限的ID号</param>
/// <param name="usersId">用户的ID号</param>
/// <returns>bool</returns>
private static bool isExistRight(int rightId,int usersId)
{
return returnValue.isExistRight(rightId, usersId);
}
/// <summary>
/// 用户是否存在
/// </summary>
/// <param name="userName">用户名称</param>
/// <returns>bool</returns>
private static bool isExistUsername(string userName)
{
return returnValue.isExistUsername(userName);
}
private static int getUserId(string userName)
{
return returnValue.getUserNameID(userName);
}
/// <summary>
/// 设置用户权限
/// </summary>
/// <param name="rightValue">权限值</param>
/// <param name="userName">用户名</param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static int setUserRight(string rightValue,string userName,int status)
{
int count = 0;
if (isExistUsername(userName) == true)
{
int usersId = getUserId(userName);
string[] RightId = rightValue.Split(new char[] { '~' });
int max = RightId.Length - 1;
for (int i = 0; i < max; i++)
{
if (isExistRight(int.Parse(RightId[i]), usersId) == true)
{
if (status == 2)
{
//删除操作
deleteOperator.deleteUserRight(int.Parse(RightId[i]), usersId);
}
}
else
{
if (status == 1)
{
addOperator.addUserRight(usersId, int.Parse(RightId[i]));
}
}
}
count = 1;
}
else
{
count = 2;
}
return count ;
}
/// <summary>
/// 所有权限
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public static string allRightValue()
{
return returnValue.allRightValue();
}
/// <summary>
/// 显示用户权限
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string ShowUserRight(string userName)
{
int userId = getUserId(userName);
return returnValue.ShowUserRight(userId);
}
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 Admin_usersRight : adminBasePase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_usersRight));
}
}
/// <summary>
/// 添加用户权限
/// </summary>
/// <param name="userId">用户ID号</param>
/// <param name="rightValue">权限值</param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static int addUserRight(int userId,int rightValue)
{
int count = 0;
if (returnValue.userRightIsExist(userId, rightValue) == true)
{
}
else
{
if (addOperator.addUserRight(userId, rightValue) == true)
{
count = 1;
}
else
{
count = 2;
}
}
return count;
}
/// <summary>
/// 是否存在此权限
/// </summary>
/// <param name="rightId">权限的ID号</param>
/// <param name="usersId">用户的ID号</param>
/// <returns>bool</returns>
private static bool isExistRight(int rightId,int usersId)
{
return returnValue.isExistRight(rightId, usersId);
}
/// <summary>
/// 用户是否存在
/// </summary>
/// <param name="userName">用户名称</param>
/// <returns>bool</returns>
private static bool isExistUsername(string userName)
{
return returnValue.isExistUsername(userName);
}
private static int getUserId(string userName)
{
return returnValue.getUserNameID(userName);
}
/// <summary>
/// 设置用户权限
/// </summary>
/// <param name="rightValue">权限值</param>
/// <param name="userName">用户名</param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static int setUserRight(string rightValue,string userName,int status)
{
int count = 0;
if (isExistUsername(userName) == true)
{
int usersId = getUserId(userName);
string[] RightId = rightValue.Split(new char[] { '~' });
int max = RightId.Length - 1;
for (int i = 0; i < max; i++)
{
if (isExistRight(int.Parse(RightId[i]), usersId) == true)
{
if (status == 2)
{
//删除操作
deleteOperator.deleteUserRight(int.Parse(RightId[i]), usersId);
}
}
else
{
if (status == 1)
{
addOperator.addUserRight(usersId, int.Parse(RightId[i]));
}
}
}
count = 1;
}
else
{
count = 2;
}
return count ;
}
/// <summary>
/// 所有权限
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public static string allRightValue()
{
return returnValue.allRightValue();
}
/// <summary>
/// 显示用户权限
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string ShowUserRight(string userName)
{
int userId = getUserId(userName);
return returnValue.ShowUserRight(userId);
}