我正在尝试从 ButtonField(数据绑定)访问文本,但如果我将其称为 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 (GridViewRow gvr in gvClass.Rows)
{
foreach (TableCell tc in gvr.Cells)
{
stringToAppend = tc.Text;
sb.Append(PRTL_UtilityPackage.FormatHtmlCharacters(stringToAppend) + " \t");
}
sb.Append("\\n");
}
当查看 ButtonField 时,foreach(gvr.Cells 中的 TableCell tc)会出现一个空字符串。