2

这是我的标记:-

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

在代码隐藏中,我试图获取当前行的文本值,但似乎无法获取;

它返回“”。

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

2 回答 2

7

您需要找到并访问控件

LinkButton btn = (LinkButton)gvDealerSupportMail.Rows[rowValue].FindControl("userList");
string result = btn.Text;
于 2015-05-05T06:52:08.780 回答
1

您可以使用 CommandSource,Command.Text和.CommandNameCommandArgument

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LinkButton commandSource = e.CommandSource as LinkButton;
        string commandText = commandSource.Text;
        string commandName = commandSource.CommandName;
        int commandArgument = Convert.ToInt32(commandSource.CommandArgument);
    }
于 2015-05-05T07:18:26.697 回答