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> ");
}