Add Link Button controls to GridView control and attach event handler

Hi there, I am kind of new to the ASP.NET.
1) I am trying to add Link Button controls to the GridView control for each row programmatically
2) Create an event handler for the link buttons
3) For each link button, pass some kind of value ( like an ID ) to the "Click" event handler

Please help!!!

public partial class UserList : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        string userSQL =
          "SELECT [ENTUserAccountId] , [FirstName] , [LastName] FROM [ENTUserAccount] ORDER BY [LastName]";
            //  Create new DataAdapter to connect to Users
            //  table in HCC_Database database

            string DBConn = DBCommon.GetConnectionString("HCC_Connection");
            using (SqlDataAdapter adapter = new SqlDataAdapter(userSQL, DBCommon.GetConnectionString("HCC_Connection")))

                DataSet ds = new DataSet();
                adapter.Fill(ds, "UserList");

                DataTable dp = ds.Tables["UserList"];
                this.GridUsers.Visible = true;

                GridUsers.DataSource = dp;

                GridUsers.HeaderRow.Cells[0].Text = "User ID";
                GridUsers.HeaderRow.Cells[1].Text = "First Name";
                GridUsers.HeaderRow.Cells[2].Text = "Last Name";


        catch (Exception ex)


    protected void GridUsers_RowCreated(object sender, GridViewRowEventArgs e)



Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Fab_CConnect With a Mentor Commented:
Hi snapsy,

Are you using an ItemTemplate in the DataGrid in your .aspx page at all? If not, I'm not sure how to help - but if so, what you need to do is the following :

In your ItemTemplate, create a column for your LinkButton, and insert the following (for example's sake I've used 'edit' as the command you wish to use) :

<asp:LinkButton runat="server" ID="LinkButton1" Text="Edit" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "UserID") %>' CommandName="edit"></asp:LinkButton>

The actual LinkButton ID doesn't matter, as it will get incremented by the DataGrid when it binds the data, and the OnClick is not needed as it will not fire inside the DataGrid.
What you need to do for it to fire an event is to add the following tag to your DataGrid control, OnItemCommand="command_name", for example :

<asp:DataGrid runat="server" ID="GridUsers" OnItemCommand="GridUsers_Command">

This is the function that will trigger when a control inside the DataGrid, like your LinkButtons, is clicked.
You will need to set up the appropriate function in your .cs file as follows :

protected void GridUsers_Command(object sender, DataGridCommandEventArgs e)
       // add code here
       // You can check the CommandName (if you have more than one) by using :
       // if (e.CommandName == "edit")
       // You can fetch the CommandArgument (the UserID in this case) by using :
       // int userID = int.Parse(e.CommandArgument.ToString());
       // Note - I use .ToString() as the CommandArgument is an 'object' - I've never actually
       // tried casting it to what it really is, I tend to just int.Parse() my IDs to be sure ^^

I hope this has helped;

All Courses

From novice to tech pro — start learning today.