Linkbutton click in a asp:GridView cell does not trigger OnRowCommand event

shaz picture shaz · Mar 6, 2013 · Viewed 14.9k times · Source

UI Feature: I have a GridView with few columns. Most important column is PcCode, which shows a string value for each row.

Expected: When I click on one of the cell from a row of that PcCode column another GridView should be displayed. However, when I am trying to use a asp:LinkButton for a cell, things just don't work. RowCommand does not get triggered when I click on a asp:LinkButton in the GridView cell. Where am I doing things wrong? Which way the expected functionality can be achieved? Thanks in advance for helping out a newbie.

In the following code I was trying to get a RowIndex and pass it through the CommandArgument and use a CommandName.

.aspx code

<asp:GridView ID="_UIProfitcenterTotalGridView" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="22" ShowFooter="True" AutoGenerateColumns="False"
    DataKeyNames="ProfitcenterCode" EnableViewState="False" 
    OnRowDataBound="_UIProfitcenterTotalGridView_RowDataBound"
    OnPageIndexChanging="_UIProfitcenterTotalGridView_PageIndexChanging" 
    OnRowCommand="_UIProfitcenterTotalGridView_OnRowCommand">
    <Columns>

       <asp:TemplateField HeaderText="PcCode" InsertVisible="False" 
            ShowHeader="False" SortExpression="ProfitcenterCode" FooterText="Total" FooterStyle-HorizontalAlign="Left">
            <ItemTemplate>
               <asp:LinkButton ID="_UIPCCodeLinkButton" runat="server" Text='<%# Eval("ProfitcenterCode") %>'  
                CommandName="Select" 
                CommandArgument='<%# ((GridViewRow) Container).RowIndex  %>'>
               </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField> 
...

code behind for aspx.cs

 protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = _UIProfitcenterTotalGridView.Rows[index];
            string ProfitcenterCode = _UIProfitcenterTotalGridView.DataKeys[_UIProfitcenterTotalGridView.SelectedIndex].Values["ProfitcenterCode"].ToString();
        }
    }

After the row is selected I need to take the selected row's value as a string and compare with a listitem to show a new GridView.

Tried

  • Using Link_Button_Click(Object sender, EventArgs e) and the following but failed.

    protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") {string ProfitcenterCode = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text; } }

Answer

shaz picture shaz · Mar 7, 2013

I tried to use LinkButton_Click() event instead of RowCommand as jason suggested:

protected void LinkButton_Click(Object sender, EventArgs e)
    {
        LinkButton button = (LinkButton)sender;
        GridViewRow row = (GridViewRow)button.NamingContainer;
        if (row != null)
        {
          string theValue = ((LinkButton)sender).CommandArgument.ToString();
          ...
          ...
          //code for the extra thing I needed to do after selecting a cell value.
        }
     }

However I still had the problem, which I figured out. The problem was the LinkButton was not binding to the rows thefore, it could not pass any value on selection. What was missing was the following code:

 if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkButton = new LinkButton();
                linkButton.Text = e.Row.Cells[0].Text; //value of the first column from the grid 
                linkButton.Enabled = true;
                linkButton.Click += new EventHandler(LinkButton_Click); //triggering the LinkButton event here. 
                e.Row.Cells[0].Controls.Add(linkButton);
            }