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 my recently made glasses background messages. The example picture is as follows: textbox and checkbox appear when you click Edit: The following is the code of each file, where red is the place to pay attention. blue is a file. If you have any misunderstanding, please point out that the younger brother is grateful for the 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 the gridview. aspx. cs file mytry objmytry = new mytry (); // instantiate void binddr () {this. gridview1.datasource = objmytry. getdate ();
This. gridview1.databind (); // data binding} protected void gridviewjavasonrowcommand (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 line of string stranswer = (textbox) gvr. findcontrol ("txtanswer"). text; // obtain the values 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 deletion 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> ");
}