获取linkbutton控件的文本内容

时间:2013-10-30 16:27:26

标签: c# asp.net

我在gridview中有linkbutton控件:

enter image description here

我正试图从这些控件获取文字:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    System.Diagnostics.Debug.WriteLine(e.Row.Cells[4].Text);
}

结果如下:

Task











 

我做错了什么,如何找到文本?

3 个答案:

答案 0 :(得分:1)

您可以使用FindControl

if (e.Row.RowType == DataControlRowType.DataRow)
{
    LinkButton btn= ((LinkButton )e.Row.FindControl("YourControlId"));
    var text = btn.Text;
    //Here you have btn object
}

答案 1 :(得分:1)

你有没有试过下面的东西?

if(e.Row.RowType == DataControlRowType.DataRow)
{
    LinkButton btn = (LinkButton)e.Row.FindControl("btn");
}

答案 2 :(得分:1)

您需要跳过标题行并仅处理数据行,如下所示:

protected void hoursReportGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    // Ignore all other rows types (header, footer, etc.) except data rows
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        System.Diagnostics.Debug.WriteLine(e.Row.Cells[4].Text);
    }
}

有关网格视图中数据行类型的详细信息,请阅读GridViewRow.RowType Property