Asp.net GridView custom template edit update cancel deletion

Source: Internet
Author: User
Tags eval getdate
Previously, the gridview control used the default functions such as editing and updating. Today, I found some information online and tried to use a custom template to edit, update, and cancel deletion. The following is a list of messages that I recently posted on the backend of the glasses network. The example image is as follows:
When you click edit, textbox and checkbox are displayed:
The code for each file is as follows, where red is the place to pay attention. Blue is the file. If you have any misunderstanding, please point out that the younger brother is grateful.
Gridview. asp tutorial x file
<Asp: gridview id = "gridview1" runat = "server" datakeynames = "id" cellpadding = "5" width = "700px" autogeneratecolumns = "false"
       
Onrowdeleting = "gridview1_ondeletecommand">
<Columns>
               
<Asp: templatefield headertext = "">
<Itemtemplate>
<Asp: button id = "delete" runat = "server" text = "delete" commandname = "delete"/>
</Itemtemplate>
</Asp: templatefield>
<Asp: templatefield headertext = "user">
<Itemtemplate>
<% # Eval ("name") %>
</Itemtemplate>
</Asp: templatefield>
<Asp: templatefield headertext = "Message">
<Itemtemplate>
<% # Eval ("question") %>
</Itemtemplate>
</Asp: templatefield>
<Asp: templatefield headertext = "reply">
<Itemtemplate>
<% # Eval ("answer") %>
</Itemtemplate>
<Edititemtemplate>
<Asp: textbox id = "txtanswer" runat = "server" text = '<% # eval ("answer") %>'> </asp: textbox>
</Edititemtemplate>
</Asp: templatefield>
              
<Asp: templatefield headertext = "review">
<Itemtemplate>
<% # Eval ("pass") %>
</Itemtemplate>
<Edititemtemplate>
<Asp: checkbox id = "pass_check" runat = "server"/>
</Edititemtemplate>
</Asp: templatefield>
<Asp: templatefield headertext = "">
<Itemtemplate>
<Asp: button runat = "server" text = "edit" commandname = "edit"/>
</Itemtemplate>
<Edititemtemplate>
<Asp: button runat = "server" text = "update" commandname = "update"/>
<Asp: button id = "cancel" runat = "server" text = "cancel" commandname = "cancel"/>
</Edititemtemplate>
</Asp: templatefield>
</Columns>
<Headerstyle backcolor = "# 4380cc" forecolor = "white" font-names = ""/>
<Rowstyle backcolor = "# eff3fb" horizontalalign = "center" font-size = "15px"/>
<Alternatingrowstyle backcolor = "# dcdedc"/>
<Pagerstyle horizontalalign = "center"/>
</Asp: gridview>
Mytry. cs file
1. public datatable getdate () {returned dataset}
2. public void update (string answer, int pass, string pk) {}// update
3. public void delete (string strid) {}// delete
Gridview. aspx. cs file
Mytry objmytry = new mytry (); // instantiate
Void binddr ()
{
This. gridview1.datasource = objmytry. getdate ();
This. gridview1.databind (); // data binding
}
Protected void gridview1_onrowcommand (object sender, gridviewediteventargs e) // triggers the editing event
    {
Gridview1.editindex = e. neweditindex; // switch to the editing page.
Binddr (); // data binding
    }
Protected void gridview1_oncancelcommand (object sender, gridviewcancelediteventargs e) // triggers the cancellation event
    {
Gridview1.editindex =-1; // return a page
Binddr (); // data binding
    }
Protected void gridview1_onupdatecommand (object sender, gridviewupdateeventargs e) // triggers an update event
    {
String pk = gridview1.datakeys [e. rowindex]. value. tostring (); // Obtain the primary key id of the current row
Gridviewrow gvr = gridview1.rows [e. rowindex]; // instantiate a row
String stranswer = (textbox) gvr. findcontrol ("txtanswer"). text; // obtain the value in the textbox
      
Try
        {
Bool is_checked = (checkbox) gvr. findcontrol ("pass_check"). checked; // Get the checkbox value
If (is_checked = true)
            {
               
Int a = convert. toint32 (is_checked); // convert bool type to integer type
Objmytry. update (stranswer, a, pk );
String c = request. urlreferrer. tostring ();
Response. redirect (c); // return to the previous page
            }
Else
            {
Int B = convert. toint32 (is_checked );
Objmytry. update (stranswer, B, pk );
String c = request. urlreferrer. tostring ();
Response. redirect (c );
            }
            
           
        }
Catch (exception ex)
        {
Alter (ex. message );
        }
    }
Protected void gridview1_ondeletecommand (object sender, gridviewdeleteeventargs e) // triggers the delete event
    {
String pk = gridview1.datakeys [e. rowindex]. value. tostring ();
Try
          {
Objmytry. delete (pk );
Alter ("deleted successfully! ");
Binddr ();
          }
Catch (exception ex)
          {
Alter (ex. message );
          }
    }
Private void alter (string strmessage) // The pop-up dialog box is displayed.
    {
Response. write ("<script> alert ('" + strmessage + "'); </script> ");
    }
Related Article

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.