Add determination deletion to DataGrid

zhaozj2021-02-16  59

Add determination of delete function for DataGrid DataGrid's function. I want to know, I encountered the following problems in actual applications, and customer requirements do a prompt before deleting. class

It seems to windows. First we all know that DataGrid supports deleting features, we can add delete columns to DataGrid, you can implement it.

Below I want to use the template column to implement the delete button with prompt. We use Northwind's sample database as an example database to operate Categories table.

DataGrid's HTML page is as follows:

Runat = "server">

Runat = "server" text = "button" commandName = "delete"> We only add a template column, Other columns are automatically generated when running. It can be seen that this template column is like deleting columns but not deleting columns, we add a common button to a common button.

= "Delete" properties. This is used to respond to the itemCommand event of DataGrid! This is like this in the delete column!

The next is the background code, the code is as follows: private Dataset DS = New DataSet (); private void page_load (Object sender, system.eventargs e) {// Place the user code here to initialize the page if (! This. ISPOSTBACK) {string strconnection = configurationSettings.Appsettings

["sa"]. toString (); sqlConnection myconnection = new sqlconnection; sqldataadapter myadapter = new SqldataAdapter ("SELECT

CategoryID, CategoryName, Description FROM Categories ", myConnection); myAdapter.Fill (ds); this.grdTest.DataSource = ds.Tables [0] .DefaultView; this.grdTest.DataKeyField =" CategoryID "; this.grdTest.DataBind ( }}

Next we add a client's onclick event to each button inside the template column. I think everyone should know the Attributes belong.

Sex! You can output the properties of the client control to the client, such as: length, color, and so on. But usually we use it to add customers

End event. Friends who know JavaScript must know CONFIRM! It will pop up a confirmation dialog box if it is determined to submit FORM, so it is not submitted, so it is natural. Private void Grdtest_ItemDatabase (Object Sender,

System.Web.UI.WebControls.DataGridItemEventArgs e) {switch (e.Item.ItemType) {case ListItemType.Item: case ListItemType.AlternatingItem: case ListItemType.EditItem: {Button btn = (Button) e.Item.FindControl ( " BTNDelete "); btn.attributes.add (" onclick "," Return Confirm ('Are you

Determine this record '); "); Break;}}} After adding this event, we need to add the following code to complete our work: Private void Grdtest_ItemCommand (Object Source,

System.Web.ui.WebControls.DataGridCommandeventAndargs e) {if (e.commandname == "delete") {this.deleterow (this.Grdtest.DataKeys [E.Item.itemindex] .tostring

());}} The above event is the incident that we excited when you click on the control inside DataGrid, we can filter out from the CommandName we think

The method to be excited deleterow (), is the code of this method: private void deleterow (string i) {string strconnection = configurationSettings.appsettings ["sa"]. Tostring

(); Sqlconnection myconnection = new sqlconnection; sqlcommand cmd = new sqlcommand ("delete from categories where

(Categoryid = " i ") ", myconnection; myconnection.open (); cmd.executenonquery (); myConnection.close ();} The above function receives a parameter, this parameter is the keyword of the current line. If you have a wrong place, please advise .e_mail: wu_jian830@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-23931.html

New Post(0)