DataList 嵌套Datagrid ,散分

NSympathizeFO 2006-08-15 10:22:55
我今天用datalist 嵌套datagrid 写一段程序.
前台代码是这样的:
<asp:Panel ID="pnlModuleContent" Runat="server">
<table width="100%" border="0">
<tr>
<td style="width: 215px">
<asp:Label id="Label5" Runat="server" resourcekey="RecordsPage" CssClass="SubHead">Records Per Page:</asp:Label>
<asp:DropDownList id="ddlRecordsPerPage" Runat="server" AutoPostBack="True">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="25">25</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
<asp:ListItem Value="100">100</asp:ListItem>
<asp:ListItem Value="250">250</asp:ListItem>
</asp:DropDownList></td>
<!--<td><dnnsc:PagingControl id="ctlPagingControl" runat="server"></dnnsc:PagingControl></TD> -->
</tr>
<tr>
<td style="width: 215px">
<asp:datalist id="dlComments" runat="server" HorizontalAlign="Center" RepeatColumns="1" OnItemDataBound ="dlComments_ItemDataBound1">
<ItemTemplate>
<table border="0">
<tr>
<td colspan="2">
<asp:label id="lblFeedbackID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FeedbackID") %>' Visible="False">
</asp:label><br></td>
<td>
<asp:label id="lblAuthor" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CreatedByEmail") %>' CssClass="head">
</asp:label>  
</td>
<td>
<asp:label id="lblMessage" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Message") %>' CssClass="Normal">
</asp:label>
</td>
<td>
<asp:label id="lblDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DateCreated") %>' CssClass="subhead">
</asp:label>
</td>
</tr>
<tr>
<table>
<tr>
<td>
<asp:datagrid ID="dgComments" runat="server" AutoGenerateColumns ="False" AllowSorting="true" HorizontalAlign="center">
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left" HeaderText="Email" SortExpression="CreatedByEmail">
<ItemTemplate>
<%#DataBinder.Eval(Container, "DataItem.CreatedByEmail")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left" HeaderText="Message">
<ItemTemplate>
<%#DataBinder.Eval(Container, "DataItem.Message")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left" HeaderText="Date" SortExpression="DateModified">
<ItemTemplate>
<%#DataBinder.Eval(Container, "DataItem.DateCreated")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</td>
</tr>
</table>
</tr>
</table>
<!--<hr width="100%" size="1">-->
</ItemTemplate>
</asp:datalist>
</td>
</tr>
</table>
</asp:Panel>


后台的dlComments_ItemDataBound1 方法 是这样的:(用VB.Net)

Protected Sub dlComments_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlComments.ItemDataBound
IndexItem = CInt(e.Item.ItemIndex.ToString)

Dim strID As String = dlComments.DataKeys(IndexItem).ToString
Dim rct As New ReplyController
Dim dgArr As ArrayList = rct.GetRepliesByFeedbackID(CInt(strID))
Dim dgStr As DataGrid = CType(e.Item.FindControl("dgComments"), System.Web.UI.WebControls.DataGrid)
dgStr.DataSource = dgArr
dgStr.DataBind()

End Sub
那个datalist绑定的代码没有贴出来,可是在这个Itemdatabound 事件中总是出现错误:
An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

可是我觉得我这段程序没有什么错误啊? 请大家指教.谢谢.
...全文
109 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
NSympathizeFO 2006-08-15
谢谢你,好累好累
回复
NSympathizeFO 2006-08-15
好了,错误已经找到了,呵呵,谢谢诸位.
回复
jimu8130 2006-08-15
粘贴出错误信息
回复
jimu8130 2006-08-15
这句话是去掉datalist表头,还有表尾,
拜托你单步执行下!看看出错行和代码
回复
NSympathizeFO 2006-08-15
顺便问一下,这句话是什么意思?
回复
NSympathizeFO 2006-08-15
不行的,我已经试过了
回复
colonzjh 2006-08-15
学习
回复
zzz22333 2006-08-15
没绑过,接分
回复
jimu8130 2006-08-15
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
不知道这样写是否正确?
回复
jimu8130 2006-08-15
那肯定是你写的这段代码有问题咯
加上
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )---这部分是c#代码,你改下
{ IndexItem = CInt(e.Item.ItemIndex.ToString)

Dim strID As String = dlComments.DataKeys(IndexItem).ToString
Dim rct As New ReplyController
Dim dgArr As ArrayList = rct.GetRepliesByFeedbackID(CInt(strID))
Dim dgStr As DataGrid = CType(e.Item.FindControl("dgComments"), System.Web.UI.WebControls.DataGrid)
dgStr.DataSource = dgArr
dgStr.DataBind() }
回复
NSympathizeFO 2006-08-15
顺便说一句:单独执行datalist 绑定是正确的,加上这个datagrid 后绑定dlComments_ItemDataBound1 就会出错.
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2006-08-15 10:22
社区公告
暂无公告