Can't get the text value of linkbutton in gridview

Soham Banerjee picture Soham Banerjee · May 5, 2015 · Viewed 7.4k times · Source

This is my markup:-

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        Width="245px" onrowcommand="GridView1_RowCommand" >
        <Columns>

            <asp:TemplateField>
            <ItemStyle BackColor="#CCCCCC" ForeColor="Black" Width="250px" HorizontalAlign="Center"
                    BorderStyle="None" />
                    <ItemTemplate>
                        <asp:LinkButton ID="userList" runat="server" CommandName="Select" CommandArgument ='<%# Container.DataItemIndex %>' Text='<%# Bind("users") %>'></asp:LinkButton>
                    </ItemTemplate>
            </asp:TemplateField>

        </Columns>        
    </asp:GridView>

And in the codebehind, I am trying to get the current row's text value, but can't seem to get it;

it returns "".

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int rowValue = Convert.ToInt32(e.CommandArgument.ToString());
    GridView1.SelectedIndex = rowValue;
    string test = GridView1.SelectedRow.Cells[0].Text;
}

Answer

fubo picture fubo · May 5, 2015

You need to find and acces the control

LinkButton btn = (LinkButton)gvDealerSupportMail.Rows[rowValue].FindControl("userList");
string result = btn.Text;