在DataGrid中选择确认后删除多行复选框列表

<%@ Page Language="C#" Debug="False" Strict="True" Explicit="True" Inherits="MultiDeleteDG.WebForm" Src="mDatagrid.aspx.cs"%>

<html>
<body>

<form runat="server">

<h3>Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo)</h3>
<br>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="white"
BorderColor="black"
CellPadding=3
CellSpacing="0"
Font-Size="9pt"
AutoGenerateColumns="False"
HeaderStyle-BackColor="darkred"
HeaderStyle-ForeColor="white"
>

<Columns>

<asp:TemplateColumn>

<HeaderTemplate>
<asp:CheckBox ID="CheckAll" OnClick="javascript: return select_deselectAll (this.checked, this.id);" runat="server"/>
<font face="Webdings" color="white" size="4">a</font>
</HeaderTemplate>

<ItemTemplate>
<asp:CheckBox ID="DeleteThis" OnClick="javascript: return select_deselectAll (this.checked, this.id);" runat="server"/>
</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn>

<HeaderTemplate>ID</HeaderTemplate>

<ItemTemplate>
<asp:Label ID="StoreID" Text='<%# DataBinder.Eval (Container.DataItem, "ID") %>' runat="server"/>
</ItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn HeaderText="Store" Datafield="Store" runat="server"/>

<asp:BoundColumn HeaderText="Address" Datafield="Address" runat="server"/>

<asp:BoundColumn HeaderText="City" Datafield="City" runat="server"/>

<asp:BoundColumn HeaderText="State" Datafield="State" runat="server"/>

<asp:BoundColumn HeaderText="Zip" Datafield="Zip" runat="server"/>

</Columns>

</ASP:DataGrid>

<br>

<asp:Button Text="Delete Items" OnClick="DeleteStore" ID="Confirm" runat="server" />

<span id="OutputMsg" EnableViewState="false" runat="server"/>

</form>

</body>


And our MultiDeleteDG.WebForm code-behind file - mDatagrid.aspx.cs:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//Import DAAB dll namespace
using Microsoft.ApplicationBlocks.Data;

namespace MultiDeleteDG
{

/// <summary>
/// Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo)
/// Author: Dimitrios Markatos - dmarko1@aol.com
/// Date: 8/2003
/// </summary>


public class WebForm : System.Web.UI.Page //Inherit Page Class
{


protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.HtmlControls.HtmlGenericControl OutputMsg; //Span Tag


protected SqlConnection objConnect;


public void Page_Load (Object Sender, EventArgs E) {

//Implement Client Side JavaScript code
string jsScript = "<script language=JavaScript> \n" +
"<!--\n" +
"function confirmDelete (frm) {\n\n" +
" // loop through all elements\n" +
" for (i=0; i<frm.length; i++) {\n\n" +
" // Look for our checkboxes only\n" +
" if (frm.elements.name.indexOf ('DeleteThis') !=-1) {\n" +
" // If any are checked then confirm alert, otherwise nothing happens\n" +
" if(frm.elements.checked) {\n" +
" return confirm ('Are you sure you want to delete your selection(s)?')\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n\n\n" +

"function select_deselectAll (chkVal, idVal) {\n" +
"var frm = document.forms[0];\n" +
"// loop through all elements\n" +
" for (i=0; i<frm.length; i++) {\n" +
" // // Look for our Header Template's Checkbox\n" +
" if (idVal.indexOf ('CheckAll') != -1) {\n" +
" // Check if main checkbox is checked, then select or deselect datagrid checkboxes \n" +
" if(chkVal == true) {\n" +
" frm.elements.checked = true;\n" +
" } else {\n" +
" frm.elements.checked = false;\n" +
" }\n" +
" // Work here with the Item Template's multiple checkboxes\n" +
" } else if (idVal.indexOf('DeleteThis') != -1) {\n" +
" // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox\n" +
" if(frm.elements.checked == false) {\n" +
" frm.elements[1].checked = false; // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox\n" +
" }\n" +
" }\n" +
" }\n" +
"}" +
"//--> \n" +
"</script>";


//Allows our .NET page to add client-side script blocks when page loads, instead of the conventional HTML JS tags.
RegisterClientScriptBlock ("clientScript", jsScript);

WebControl button = (WebControl) Page.FindControl ("Confirm");
button.Attributes.Add ("onclick", "return confirmDelete (this.form);");

objConnect = new SqlConnection ("server=(local);database=pubs;uid=sa;pwd=;");

if (!IsPostBack) {

BindData();

}


}


public void DeleteStore (Object sender, EventArgs e) {

string dgIDs = "";
bool BxsChkd = false;

foreach (DataGridItem i in MyDataGrid.Items) {

CheckBox deleteChkBxItem = (CheckBox) i.FindControl ("DeleteThis");

if (deleteChkBxItem.Checked) {

BxsChkd = true;

// Concatenate DataGrid item with comma for SQL Delete
dgIDs += ((Label) i.FindControl ("StoreID")).Text.ToString() + ",";

}

}

// Set up SQL Delete statement, using LastIndexOf to remove tail comma from string.
string deleteSQL = "DELETE from Stores WHERE stor_id IN (" + dgIDs.Substring (0, dgIDs.LastIndexOf (",")) + ")";


if (BxsChkd == true) { // Execute SQL Query only if checkboxes are checked, otherwise error occurs with initial null string

try {

SqlHelper.ExecuteNonQuery (objConnect, CommandType.Text, deleteSQL);
OutputMsg.InnerHtml += "<font size=4><b>Store information has been deleted.</b></font>";
OutputMsg.Style["color"] = "green";

} catch (SqlException err) {

OutputMsg.InnerHtml += err.Message.ToString(); //"<font size=4><b>An error occurred and the record could not be deleted</b></font>";
OutputMsg.Style["color"] = "red";

}

//Refresh data
BindData();

}

}


public void BindData() {

String sqlQuery = "Select stor_id As Id, stor_name As Store, City, State, Zip from Stores";

MyDataGrid.DataSource = SqlHelper.ExecuteDataset(objConnect, CommandType.Text, sqlQuery);
MyDataGrid.DataBind();

objConnect.Close();
objConnect = null;

}

} //End Class

}//End Namespace

转载于:https://www.cnblogs.com/xaspx/archive/2004/12/09/74914.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值