C# 如果ButtonFields文本为';它不被认为是一个表单元格吗?

C# 如果ButtonFields文本为';它不被认为是一个表单元格吗?,c#,asp.net,gridview,databound,buttonfield,C#,Asp.net,Gridview,Databound,Buttonfield,我试图从按钮字段(数据绑定)访问文本,但如果将其称为TableCell,则无法获取文本。我能做些什么来获取它的文本 标记: <asp:GridView ID="gvClass" runat="server" CssClass="grid" HeaderStyle-Font-Bold="true" Width="100%" OnSelectedIndexChanged="gvClass_SelectedIndexChanged" DataKeyNames="ClassID" AutoGene

我试图从按钮字段(数据绑定)访问文本,但如果将其称为TableCell,则无法获取文本。我能做些什么来获取它的文本

标记:

<asp:GridView ID="gvClass" runat="server" CssClass="grid" HeaderStyle-Font-Bold="true" Width="100%" OnSelectedIndexChanged="gvClass_SelectedIndexChanged" DataKeyNames="ClassID" AutoGenerateColumns="False" OnPageIndexChanging="gvClass_PageIndexChanging">
    <HeaderStyle Font-Bold="True" />
    <Columns>
        <asp:ButtonField DataTextField="ClassID" HeaderText="Class ID" CommandName="Select" />
        <asp:BoundField DataField="CourseName" HeaderText="Course Name" />
        <asp:BoundField DataField="WarehouseName" DataFormatString="Warehouse" HeaderText="Warehouse" />
        <asp:BoundField DataField="TrainerNames" HeaderText="Trainer(s)" />
        <asp:BoundField DataField="StartTime" HeaderText="Start Time" />
        <asp:BoundField DataField="Duration" HeaderText="Duration" />
        <asp:BoundField DataField="Course Category" HeaderText="Course Category" />
        <asp:BoundField DataField="Comment" HeaderText="Comment" />
    </Columns>
    <EmptyDataTemplate>
        <span class="italic grey">No Record Found</span>
    </EmptyDataTemplate>
</asp:GridView>

foreach(gvr.Cells中的TableCell tc)在查看按钮字段时会出现一个空字符串。

在按钮字段中,单元格内还有另一个控件,您需要为其获取文本:

Control control = gvr.Cells[0].Controls[0];
string text = string.Empty;
if (((ButtonField)gvClass.Columns[0]).ButtonType ==
    ButtonType.Link)
{
    LinkButton btn = control as LinkButton;
    text = btn.Text;
}
else
{
    Button btn = control as Button;
    text = btn.Text;
}

在ButtonField中,单元格内还有另一个控件,您需要为其获取文本:

Control control = gvr.Cells[0].Controls[0];
string text = string.Empty;
if (((ButtonField)gvClass.Columns[0]).ButtonType ==
    ButtonType.Link)
{
    LinkButton btn = control as LinkButton;
    text = btn.Text;
}
else
{
    Button btn = control as Button;
    text = btn.Text;
}

您仍然可以使用tablecell方法:

protected void myGridView_DataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // You may have to play with the index of the control
        // I have found that often a GridView will place a 
        // Literal control as Control[0] and the LinkButton
        // as Control[1]. Once you find the index, it won't
        // change.
        LinkButton btn = (LinkButton)e.Row.Cells[0].Controls[1];
        string text = btn.Text;
    }
}

您仍然可以使用tablecell方法:

protected void myGridView_DataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // You may have to play with the index of the control
        // I have found that often a GridView will place a 
        // Literal control as Control[0] and the LinkButton
        // as Control[1]. Once you find the index, it won't
        // change.
        LinkButton btn = (LinkButton)e.Row.Cells[0].Controls[1];
        string text = btn.Text;
    }
}
例如:


示例:

什么是
gvr
?这是GridView的名称吗?什么是
gvr
?这是GridView的名称吗?