【问题标题】:Can't get the text value of linkbutton in gridview无法在gridview中获取linkbutton的文本值
【发布时间】:2017-11-15 20:11:04
【问题描述】:

这是我的标记:-

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

【问题讨论】:

  • 以上代码在GridView1.SelectedRow处给出Index was out of range.异常。这是完整的 html 标记吗?

标签: c# asp.net gridview asplinkbutton


【解决方案1】:

您需要找到并访问该控件

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

【讨论】:

  • 你也可以使用var lnkSelect = (LinkButton)sender; var xyz = (lnkSelect.Text);
【解决方案2】:

您可以使用CommandSource 获取Command.TextCommandNameCommandArgument

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多