Wednesday, 2 October 2013

ButtonField class in ASP.NET

ButtonField is very well known field of ASP.NET Gridview control.
We have seen how to use element to display select, edit and delete buttons in a GridView control. However, if we wish to provide buttons which perform additional custom functionalities, we use the element in a GridView control.
It's part of Data events like DetailView and Gridview under 
<asp:ButtonField ButtonType...... />

Description With Example :

In the below example, we have used the ButtonField element to add Details button in a row of GridView control. The element displays the button as a link or a button or an Image.

Step 1:

Declare a GridView control and set the AutoGenerateColumns property to 'false' and set the event handler for the RowCommand event.

Step 2:

When we click on the Details button, the GridView controls row command event is raised. In this example, the event RowCommand is handled by the GridView1_RowCommand event.
<asp:GridView ID="GridView1"  DataSourceId="MyDataSource"
DataKeyNames="BlogId,FollowerName" ShowFooter="true" AutoGenerateColumns="false"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
 <asp:BoundField  DataField="BlogId" HeaderText="Blog Code" />
    <asp:BoundField  DataField="FollowerName" HeaderText="FollowerName" />
 <asp:BoundField  DataField="FollowerDetails" HeaderText="Description" />
 <asp:buttonfield  buttontype="button" Text="Row Details" commandname="Details"  />


Step 3:

For row command , use following code or use can use RowCreated , RowUpdating etc gridviewrow events

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        int index = Int32.Parse((string)e.CommandArgument);
        string Code = (string)GridView1.DataKeys[index].Values["BlogId"];
        string Name = (string)GridView1.DataKeys[index].Values["FollowerName"];

Above post is just is simple example for beginner to use it.. ...
We can do onclick events by ModelPopupExtender of Ajax toolkit...

[ Quote : "For the unlearned, old age is winter; for the learned it is the season of the harvest." ]



No comments:

Post a Comment