
这是我尝试过的代码和解决方案:
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;
要查看您的单元格是否在调试模式下具有任何值(在调试输出窗口中检查文本):
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部分 – 访问DataItem
DataBinder.Eval(e.Row.DataItem,"Columnname")
最后,我不确定您使用匿名类型做了什么,但在访问属性之前可能需要检查内容:
if(MyControl.GetType() == typeof(Hyperlink)){ Hyperlink Testlink = (Hyperlink)MyControl; Testlink .Visible = false;} 总结 以上是内存溢出为你收集整理的c# – GridView RowDataBound处理程序 – 无法从行获取数据全部内容,希望文章能够帮你解决c# – GridView RowDataBound处理程序 – 无法从行获取数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)