Common gridview operations

Source: Internet
Author: User

1. Edit in the gridview:
Add a template column, put a linkbutton in the itemtemplate, and set the commandname attribute to "edit ". Edit the rowediting event of the gridview: gridview1.editindex = E. neweditindex; then bind the gridview. Note: Set readonly = "true" for columns that do not need to be edited"

Update:
Put a linkbutton in the edit item template and set the commandname attribute to "Update ". Edit the rowupdating event of the gridview:
(1) gridviewrow gvr = gridview1.rows [E. rowindex]; // obtain the updated row
(2) gridview1.columns [2]. extractvaluesfromcell (E. newvalues, gvr. cells [2] As datacontrolfieldcell, datacontrolrowstate. Edit, true); // obtain the updated value of column 3rd
(3) var price = E. newvalues ["price"]. tostring (); // assign the updated value of the price field (column 3rd) to the variable price.
(4) model = new model (); // instantiate an object
// Assign a value
Model. ID = gridview1.datakeys [E. rowindex]. value. tostring (); // update the key value of the row. Note: datakeynames of the gridview must be set.
Model. Price = price; // edited Column
Model. Name = gvr. cells [1]. Text; // columns that do not need to be edited (2nd columns)
Update (model); // call the update Method
Gridview1.editindex =-1; // make the gridview not updated
Bindall (); // bind

Cancel:
Put a linkbutton in the edititemtemplate and set the commandname attribute to "cancel ". Edit the rowcancelingedit event of the gridview: gridview1.editindex =-1; then bind the gridview.

Or add a commandfield to write, edit, update, and cancel the event.

 

2. Delete in the gridview:
Add a commandfield deletion and edit the rowdeleting event:
String bookid = gridview1.datakeys [E. rowindex]. value. tostring (); // obtain the key value of the deleted Column
Delete (bookid); // call the delete Method
Bindall (); // bind

3. Text = '<% # getpublisher. getpublishers (eval ("publisherid"). tostring () %>' // bind getpublisher class to the getpublishers () method

4. showmodaldialog
// Method
Function showdialog (SID)
{
Window. showmodaldialog ('khxx _ child. aspx? Id = '+ Sid + '', window, 'dialogwidth = 600px; dialogheight = 500px ');
}
Onclick = "showdialog ('<% # eval (" ID "). tostring () %>')" // bind the HTML Control
Onclientclick = <% # "showdialog ('" + eval ("ID"). tostring () + "')" %> // bind a Server Control
// Close modaldialog and jump to the original page:
Response. Write ("<SCRIPT> alert ('modification successful! '); Window. Close (); </SCRIPT> ");
In the original page command, enter enableeventvalidation = "false". When loading the page, do not write it! Ispostback

 

5. The confirm deletion dialog box is displayed:
Set the onclientclick attribute of the delete button: Return confirm ('Are you sure you want to delete it? ')

 

6. cursor Effect
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
E. Row. Attributes. Add ("onmouseover", "currentcolor = This. style. backgroundcolor; this. style. backgroundcolor = '# 6699ff '");
E. Row. Attributes. Add ("onmouseout", "This. style. backgroundcolor = currentcolor ");
}
}

 

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.