GridView Edit, Cancel button custom control

Source: Internet
Author: User

This demand came from a forum friend raised the question, he would like to be able to customize the edit, cancel the button, rather than the GridView with the editing and cancellation. This is only a point to propose some solutions.

First set up a GridView in the foreground of the page.

<Div>      <Asp:gridviewID= "GridView1"runat= "Server">          <Columns>              <Asp:templatefieldHeaderText= "Action">                  <ItemTemplate>                      <Table>                          <TDAlign= "Center">                              <Asp:buttonID= "Edit"runat= "Server"Text= "Edit"Visible= "true"OnClick= "Edit_click"CommandArgument= "<%# container.dataitemindex%>" />                              <Asp:buttonID= "Cancel"runat= "Server"Text= "Cancel"Visible= "false"OnClick= "Cancel_click" />                          </TD>                      </Table>                  </ItemTemplate>              </Asp:templatefield>          </Columns>      </Asp:gridview>  </Div>  

Notice here that I set a Dataitemindex value for the CommandArgument property of the button edit, which is the index value of the default row. This parameter allows you to get the line number of the GridView.

Then I bind the data source when the homepage is loaded.

protected voidPage_Load (Objectsender, EventArgs e) {      if(!page.ispostback) {DataTable dt=NewDataTable (); Dt. Columns.Add ("ID",typeof(int)); Dt. Columns.Add ("name",typeof(string)); Dt. Rows.Add (10001,"guwei40371"); Dt. Rows.Add (10002,"guwei40372");  This. Gridview1.datasource =dt.          DefaultView;  This.      Gridview1.databind (); }  }  

Here is a simple, binding two columns to the GridView binding.

Next two button events:

protected voidEdit_click (Objectsender, EventArgs e) {      intindex = Convert.ToInt32 (sender asButton). CommandArgument);//get to line numberButton Button = This. Gridview1.rows[index]. FindControl ("Cancel") asButton;//Find the Cancel button for the current linebutton. Visible =true;//Set the button's visible to True}    protected voidCancel_click (Objectsender, EventArgs e) {      intRow = ((GridViewRow) ((Button) sender). NamingContainer). RowIndex;//the rowindex of the naming container (GridViewRow) is found directly via the buttonResponse.Write ("<script>alert ('"+ This. Gridview1.rows[row]. cells[1]. Text +"') </script>");//directly pops up the contents of the current row cell index to 1}

The meaning of the specific code, which is already annotated, is not repeated here.

Finally, look at the effect of execution.

When you click the Edit button, the Cancel button is displayed.


When you click the Cancel button, it pops up 10001.

GridView Edit, Cancel button custom control

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.