js代码:
<script type="text/javascript">
//全选/反选function GetAllCheckBox(CheckAll){
var items=document.getElementsByName("CB_Group");
for(var i=0;i<items.length;i++){
if(items[i].type=="checkbox"){
//全选的CheckBox.checked为true则为为全选,false则反选
items[i].checked=CheckAll.checked;
}
}
}
//检查是否至少选择了一项
function CheckHasSelectedItem()
{
//得到所有name为CB_Group
var checkbox = document.all.CB_Group;
if(checkbox==null)
{
return false;
}
if(checkbox.length+""!="undefined")
{
for( var i=0;i<checkbox.length;i++ )
{
if(checkbox[i].checked)
{
return true;
}
}
}
/*当数据库中只有一条记录时,
*document.all.CB_Group取出的是Object对象而不是数组
所以我们在这还要判断得到Object对象时是否被选中
(可以在数据库中只去一条数据测试(top 1),查看是否有这个小Bug)
*/
else if(checkbox.checked)
{
return true;
}
else
{
return false;
}
}
//删除
function ConfirmDelete()
{
if(CheckHasSelectedItem())//如果至少选择了一项
{
return confirm("Are you sure to delete the selected item?");
}
else
{
alert("Please choose at least one!");
return false;
}
}
//修改
function ConfirmUpdate()
{
if(CheckHasSelectedItem())
{
//return confirm("Are you sure to update the selected item?")
if (!confirm("Are you sure to update the selected item?"))
return false;
}
else
{
alert("Please choose at least one!");
return false;
}
}
</script>
.aspx代码:
<asp:LinkButton ID="lbtn_UpdateUser" ForeColor="White" runat="server" OnClientClick="return ConfirmUpdate()"
OnClick="lbtn_UpdateUser_Click">Batch Update | </asp:LinkButton>
<asp:LinkButton ID="lbtn_DeleteUser" ForeColor="White" runat="server" OnClientClick="return ConfirmDelete()"
OnClick="lbtn_DeleteUser_Click">Delete</asp:LinkButton>
或者在.aspx.cs代码里这样写:
//删除代码:
string[] cbvalue = Request["CB_Group"].Split(',');
for (int i = 0; i < cbvalue.Length; i++)
{
string userid = cbvalue[i];
int uid = Convert.ToInt32(userid);
if (FieldValueDB.DelByUid(uid) > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "result", "alert('Delete successfully!');", true);
BindFieldName();//重新绑定GridView
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "result", "alert('Failed to delete!');", true);
}
}
//修改代码
/// <summary>
/// 将数组转换为字符串
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public string getString(string[] arr)
{
string str = "";
for (int i = 0; i < arr.Length; i++)
{
str = str + arr[i] + ",";
}
str = str.Substring(0, str.Length - 1);
return str;
}
try
{
string[] cbid = Request["CB_Group"].Split(',');
Session["cbuserid"] = getString(cbid);
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "result", "alert('Please choose at least one');", true);
return;
}
Response.Redirect("UpdateUserInfo.aspx");