Question

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; } }

Was it helpful?

Solution

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);
            }

OTHER TIPS

You can use the sender object ( the link button that calls the event) and get the parent row from the gridview. Example:

Protected Sub linkButton_click(ByVal sender As Object, ByVal e As EventArgs)
   'First cast the sender to a link button
   Dim btn As LinkButton = CType(sender, LinkButton)
   'now, if there is a command arguement, you can get that like this
   Dim id As String = btn.CommandArgument
   'to get the gridviewrow that the button is on:
    Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)   

End Sub

It was hard to follow exactly what you were looking for, so if i missed something let me know and I will add it.

protected void linkButton_click(object sender, EventArgs e)
{
    //First cast the sender to a link button
    LinkButton btn = (LinkButton)sender;
    //now, if there is a command arguement, you can get that like this
    string id = btn.CommandArgument;
    //to get the gridviewrow that the button is on:
    GridViewRow row = (GridViewRow)btn.NamingContainer;

}

And change your linkbutton to:

 <asp:LinkButton OnClick="linkButton_click" ID="_UIPCCodeLinkButton"
 runat="server" Text='<%# Eval("ProfitcenterCode") %>'

            CommandName="Select" 
            CommandArgument='<%# ((GridViewRow) Container).RowIndex  %>'>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top