asp.net message box

Source: Internet
Author: User
asp.net

The function of the DataGrid I think we all know that I encountered the following problems in the actual application, the customer asked to make a prompt before the deletion. Similar to Windows. First we all know that the DataGrid supports deletion, so we can add a delete column to the DataGrid to implement it.

Adding a Delete-determined feature to the DataGrid
The function of the DataGrid I think we all know that I encountered the following problems in the actual application, the customer asked to make a prompt before the deletion. Class

Like Windows. First we all know that the DataGrid supports deletion, so we can add delete columns to the DataGrid.

Next I want to use the template column to implement the Delete button with hints. We use the sample database of Northwind as an example database to manipulate categories tables.

The contents of the HTML page of the DataGrid are as follows:
<asp:datagrid id= "Grdtest" style= "Z-INDEX:101; left:205px; Position:absolute; Top:134px "

runat= "Server" >
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:button id= "Btndelete"

runat= "Server" text= "button" Commandname= "Delete" ></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
We added only one template column, and the other columns were automatically generated at run time.
You can see that this template column is very much like deleting a column but not deleting a column, we added a commandname to an ordinary button

= property of ' Delete '. This is used to respond to the ItemCommand event of the DataGrid! That's what's inside the Delete column!

The next step is the background code, and the code looks like this:
Private DataSet ds = new DataSet ();
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (!this. IsPostBack) {
String strconnection = ConfigurationSettings.AppSettings

["sa"]. ToString ();
SqlConnection myconnection = new SqlConnection (strconnection);
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 in the template column. I think we should all change the attributes.

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

End event. The friend who knows JavaScript must know confirm! It will pop up a confirmation dialog box if you are sure to submit form otherwise you won't

Submit, so it's natural to use this.
private void Grdtest_itemdatabound (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") (' Do You

To determine the deletion of this record ");
Break
}
}
}
After adding this event, we will need to add the following code to complete our work:
private void Grdtest_itemcommand (object source,

System.Web.UI.WebControls.DataGridCommandEventArgs e) {
if (E.commandname = = "Delete") {
This. DeleteRow (This.grdtest.datakeys[e.item.itemindex]. Tostring

());
}
}
The above event is the event that we fire when we click on the control in the DataGrid, we can filter it through commandname we want to

The method to be fired DeleteRow (), which is the code for this method:
private void DeleteRow (string i) {
String strconnection = configurationsettings.appsettings["sa"]. Tostring

();
SqlConnection myconnection = new SqlConnection (strconnection);
SqlCommand cmd = new SqlCommand ("DELETE from Categories WHERE

(CategoryID = "+i+") ", MyConnection);
Myconnection.open ();
Cmd. ExecuteNonQuery ();
Myconnection.close ();
}
The above function receives a parameter, which is the keyword for the currently selected row.
Please advise the wrong place. E_mail:wu_jian830@hotmail.com




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.