Use of buttons in the GridView row

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/hongdi/article/details/6455947

Use of buttons in the GridView row

In the course of a Web project, especially when you develop an ASP. NET application, you often use the data display space of the GridView control, and then you need to place a button or LinkButton at the end of each line to manipulate the current row. For example, add a Delete button to delete the row's record when the button is clicked.

This article will provide 5 common methods to implement the deletion of row records, and thus to understand the use of buttons in the next GridView.

Method 1:

Place a template column in the GridView control, place a button inside it, and bind the button with the primary key value, with the following code:

<asp:templatefield headertext= "Method 1" >

<ItemTemplate>

<asp:button id= "Btndelone" runat= "server" commandargument= ' <%# Eval ("ID")%> '

onclick= "Btndelone_click" text= "Delete"/>

</ItemTemplate>

</asp:TemplateField>

and the button to add the onclick click event handler:

protected void Btndelone_click (object sender, EventArgs e)

{

Button btn= (button) sender;//Note the conversion of the control type

String id=btn.commandargument;//gets the corresponding value for the control binding

Deletebyid (ID);//deletebyid is a function that is deleted based on the ID process

}

Method 2:

Open the Smart label of the GridView--edit column, add a ButtonField, and set the corresponding property worth,

Note that the CommandName property here sets him to Del, and then adds the Rowcommand event to the GridView space, which indicates that the event is fired when generating events in the GridView. Its event handling is:

protected void Grvdata_rowcommand (object sender, Gridviewcommandeventargs e)

{

This function is also called by the Delete button

if (E.commandname = = "Del")

{

Note that commandargument at this point represents the index of the row in which it is located.

int Index=int. Parse (E.commandargument.tostring ());

String id = Grvdata.rows[index]. Cells[0]. Text;

Deletebyid (ID);

}

}

Method 3:

Method 3 is similar to Method 2, in fact its underlying principle is consistent, method 3 of the button can be interpreted as a method of 2 kinds of buttons of the embodiment (let the button more clearly what to do). Its settings

for which a button event is added, this event is the GridView Space RowDeleting event, which is fired when the delete command is executed against the data source. If you do not write this event and use this button, you will receive an error during use. Add time processing for this to:

protected void Grvdata_rowdeleting (object sender, Gridviewdeleteeventargs e)

{

String id = Grvdata.rows[e.rowindex]. Cells[0]. Text;

Deletebyid (ID);

}

Note that this differs from Method 2, where Method 2 obtains the row index through the e.commandargument of the button, whereas in this method the value is obtained directly using the row index provided by Gridviewdeleteeventargs E. And the CommandName of the button of Method 2 is our custom del, and this method is the delete, Microsoft has helped us set a value in advance, so when it fires it will first execute the Rowcommand event in Method 2, Then execute the RowDeleting event, so you can also combine method 2 and Method 3 into an event handler function, but rowdeleting must have, but there is nothing in the content to write.

Method 4:

The basic configuration and use of Method 3 and Method 4 are basically consistent, but they were only designed to better differentiate what they were doing. This method selects the Add "select" button,

Its event handler function is similar to Method 3, as follows:

protected void Grvdata_selectedindexchanged (object sender, EventArgs e)

{

Method 1, directly past the value of the numeric key

String id = grvData.SelectedDataKey.Value.ToString ();

Method 2, gets the index of the selected row, and gets the value by index

String id = Grvdata.rows[grvdata.selectedindex]. Cells[0]. Text;

Deletebyid (ID);

}

Method 4 The Processing room method also provides 2 methods to obtain the corresponding value. A follow method of 3 types is the row index where the row is obtained. Method 4 also has one way to get the value of the number key of the row directly. Method 4 will also invoke Rowcommand when executing, just at this point the CommandName value becomes select.

Method 5:

Method 5 is a more traditional approach, which is to add a link to each line, pass a URL parameter in the link, get the URL parameter in the delete page, and perform the corresponding delete operation ... This is relatively simple and will not repeat.

Here are some 4 ways to compare calls:

OnClick

Rowcommand

RowDeleting

SelectedIndexChanged

Method 1

0

Method 2

0

Method 3

0

0

Method 4

0

0

0 means that it will be dropped, we found that method 3 and Method 4 will make 2 calls, and from the Design Method 3 Method 4, to use Microsoft to provide convenient buttons, in addition to the excitation Rowcommand processing method, but also to stimulate the corresponding special operations, such as delete and select and so on. Basic or Recommended Method 1, Method 2, Method 5 is also a good choice.

Use of buttons in the GridView row

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.