c# - GridView RowDataBound 处理程序 - 无法从行中获取数据

标签 c# .net asp.net gridview

我有一个匿名类型的 GridView。我需要检查单元格中的值并在满足条件时突出显示该单元格。问题是,当我尝试从行的单元格中提取数据时,总是得到一个空字符串。我已成功突出显示所有单元格,并且已在 Visual Studio 2010 调试器中检查并确认该行具有数据(该行的 DataItem 具有我需要的值)。这是在 PostBack 上发生的,我不确定这是否是个问题。

这是我尝试过的代码和解决方案:

protected void grvValidCourses_RowDataBound(Object sender, GridViewRowEventArgs e) {
 if (e.Row.RowType == DataControlRowType.DataRow) {
  String str = e.Row.Cells[6].Text.ToString(); // empty string
  Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null
  DataRowView rowView = (DataRowView)e.Row.DataItem; // exception about casting anonymous type

这是怎么回事?为什么我无法从单元格中获取数据?

GridView 的标记:

 <asp:GridView ID="grvValidCourses" runat="server" Width="790px" OnRowCancelingEdit="grvValidCourses_RowCancelingEdit"
                    OnRowEditing="grvValidCourses_RowEditing" OnRowUpdating="grvValidCourses_RowUpdating" OnRowDeleting="grvValidCourses_RowDeleting"
                    AutoGenerateColumns="False" OnSelectedIndexChanged="grvValidCourses_SelectedIndexChanged"
                    OnRowDataBound="grvValidCourses_RowDataBound" >
                    <Columns>
                        <asp:CommandField ShowEditButton="True" EditText="Edit" UpdateText="Update |" />
                        <asp:TemplateField ShowHeader="False">
                             <ItemTemplate>
                               <asp:LinkButton ID="lbnDelete" runat="server" CausesValidation="False" CommandName="Delete"
                                    Text='<%# (Eval("active") == null ? "Delete" : ((Eval("active").ToString() == "0" ? "Restore" : "Delete"))) %>' />  
                             </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowSelectButton="True" SelectText="Details" />
                        <asp:TemplateField HeaderText="Training Name" SortExpression="coursename">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("coursename") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("coursename") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="ttNo" HeaderText="#" SortExpression="ttNo" ReadOnly="True" />
                        <asp:TemplateField HeaderText="Course Date" SortExpression="startDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("startdate", "{0:M/d/yyyy}") %>'></asp:TextBox>
                                <asp:CalendarExtender ID="TextBox3_CalendarExtender" runat="server" Enabled="True"
                                    TargetControlID="TextBox3" Format="M/d/yyyy">
                                </asp:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("startdate", "{0:M/d/yyyy}") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Expiry Date" SortExpression="endDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("enddate", "{0:M/d/yyy}") %>'></asp:TextBox>
                                <asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True"
                                    TargetControlID="TextBox1" Format="M/d/yyyy">
                                </asp:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblEndDate" runat="server" Text='<%# Bind("enddate", "{0:M/d/yyyy}") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>No valid courses found.</EmptyDataTemplate>
                </asp:GridView>

更新:我一直在尝试您的所有建议,但我遇到了异常、空值或空字符串。我什至在一个更简单的示例中重新创建了问题,但仍然无法弄清楚!不过,我会继续努力,感谢任何新建议。

最佳答案

第 1 部分 - 缺失文本

由于需要访问子控件,您可能在第一部分得到空白值:

String str = ((DataBoundLiteralControl)e.Row.Cells[6].Controls[0]).Text;

在 Debug模式下查看您的单元格是否有任何值(检查调试输出窗口中的文本):

void grvValidCourses_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    System.Diagnostics.Trace.WriteLine(e.Row.Cells.Count);
     foreach (TableCell c in e.Row.Cells)
     {
      System.Diagnostics.Trace.WriteLine(c.Text);
     }
  }
}

第 2 部分 - 缺少控制

这是错误的:

Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null

您不能在 gridview 中搜索行的控件。您需要搜索该行。

Label lblProductOptionGrpName = (Label)e.Row.FindControl("lblProductOptionGrpName");

第 3 部分 - 访问数据项

DataBinder.Eval(e.Row.DataItem, "ColumnName")

最后,我不确定您对匿名类型做了什么,但您可能需要在访问属性之前检查内容:

if(MyControl.GetType() == typeof(HyperLink))
{
  HyperLink TestLink = (HyperLink)MyControl;
  TestLink .Visible = false;
}

关于c# - GridView RowDataBound 处理程序 - 无法从行中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741220/

相关文章:

c# - 在 C# 中使用 using 语句强制执行完整命名空间

c# - 获取特定日期格式的 ID 号开始

c# - 在 CentOS 或 Linux 上使用 Mono 执行 .Net 应用程序

c# - 检索物理核心处理器的数量

c# - 如何使用系统命名空间创建扩展方法

css - 当元素是 selectable=false 时,如何将样式应用于 asp.net 菜单

c# - 在 UpdatePanel 之外更新控件

c# - 为什么合作取消任务的状态是 "Faulted"而不是 "Canceled"?

c# - 在 C# 中比较 2 个价格作为数据类型字符串

ASP.NET InProc 缓存与分布式缓存