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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.