溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

asp.net如何實現(xiàn)全部選中與取消操作

發(fā)布時間:2021-10-13 10:01:58 來源:億速云 閱讀:233 作者:小新 欄目:web開發(fā)

這篇文章主要介紹asp.net如何實現(xiàn)全部選中與取消操作,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

前臺代碼


:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<script language="javascript" type="text/javascript" src="ajax/jquery.js"></script>
<script language="javascript" type="text/javascript">
//頁面的初始化
$(document).ready( function(){ loadUserInfo(); });
//加載用戶信息到頁面
function loadUserInfo()
{
$.ajax(
{
type:"POST",
url:"Default.aspx",
data:{action:'action'},
success:loadUserInfoCallbace
}
);
}
//頁面初始化回調(diào)函數(shù)
function loadUserInfoCallbace(r)
{
if(r=="")
{
$("#userInfo").html("暫無數(shù)據(jù)");
}
else
{
$("#userInfo").html(r);
}
}
//全部選中
function CheckAll(obj)
{
$("input[@type=checkbox][@name=checkItem]").attr("checked",$(obj).attr("checked"));
}
//收集所有選中項
function NumberID()
{
var allcheckboxs=$("input[@type=checkbox][@name=checkItem][checked]");
var ids="";
for(i=0;i<allcheckboxs.length;i++)
{
var id=$(allcheckboxs[i]).attr("id").split("_")[1];
ids+=id;
ids+=",";
}
return ids;
}
//刪除用戶
function DeleteUser()
{
if(!window.confirm("您真的要刪除選中用戶信息嗎?"))
{
return;
}
var ids=NumberID();
$.ajax({
type:'POST',
url:'Default.aspx',
data:{action:'Delete',userid:ids},
success:deleteUserCall
});
}
//刪除用戶回調(diào)函數(shù)
function deleteUserCall(r)
{
if(r=="ok")
{
alert("刪除成功");
loadUserInfo();
}
else
{
alert("失敗");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div  id="userInfo">
</div>
</form>
</body>
</html>


后臺代碼:

復(fù)制代碼 代碼如下:


using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
string Straction = "";
protected void Page_Load(object sender, EventArgs e)
{
Straction = Request["action"];
if(Straction=="action")
{
UserInfo();
}
if (Straction == "Delete")
{
DeleteUser();
}
}
/// <summary>
/// 周昕 2009-6-8號加載用戶詳細信息
/// </summary>
public void UserInfo()
{
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
string sql = "select * from loginuser";
SqlCommand mycom = new SqlCommand(sql, mycon);
mycon.Open();
SqlDataReader myda = mycom.ExecuteReader();
StringBuilder str = new StringBuilder();
str.Append("<table><tr><td><input id='checkall' name='checkall' type='checkbox' onclick='CheckAll(this)'/>全選</td><td>用戶名</td><td>用戶全名</td></tr>");
while (myda.Read())
{
str.Append("<tr><td>");
str.Append("<input id='checkItem_" + myda["ID"].ToString() + "' type='checkbox' name='checkItem' style='text-align='left' onclick=' NumberID()'/></td>");
str.Append("<td>" + myda["UserName"].ToString() + "</td>");
str.Append("<td>" + myda["FullName"].ToString() + "</td></tr>");
}
str.Append("</table>");
str.Append(" <div style=' text-align:center'><input type='button' value='刪除' onclick='DeleteUser()' /></div>");
Response.Clear();
Response.ContentType = "application/text";
Response.Write(str);
Response.End();
}
/// <summary>
/// 周昕 2009-6-8 刪除選中用戶的詳細信息
/// </summary>
public void DeleteUser()
{
//獲取用戶ID
string strID = Request["userid"];
string Userid = strID.Substring(0, strID.Length - 1);
//轉(zhuǎn)換成為數(shù)組
string[] stridArray = Userid.Trim().Split(',');
string sql = "delete from loginuser where ID='" + stridArray[0].ToString() + "'";
for (int i = 0; i < stridArray.Length; i++)
{
string id = stridArray[i].ToString();
sql += "or ID='" + id + "'";
}
SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
mycon.Open();
SqlCommand mycom = new SqlCommand(sql, mycon);
int n = (int)mycom.ExecuteNonQuery();
mycon.Close();
if (n > 0)
{
Response.Clear();
Response.ContentType = "application/text";
Response.Write("ok");
Response.End();
}
else
{
Response.Clear();
Response.ContentType = "application/text";
Response.Write("no");
Response.End();
}
}
}

以上是“asp.net如何實現(xiàn)全部選中與取消操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI