datalist中嵌套datalist 子datalist的ItemCommand事件
我需要取得datalist中子datalist的id值.
在DataList2_ItemCommand事件中这么写
int id = Convert.ToInt32(DataList2.DataKeys[e.Item.ItemIndex].ToString());//datalist2未定义
DataList dl = (DataList)this.DataList1.FindControl("DataList2");//这里得到的dl是null
int id = Convert.ToInt32(dl.DataKeys[e.Item.ItemIndex].ToString());//未将对像引用到实例
前台:
<asp:DataList ID="DataList1" runat="server" onclick="objgetcheckbox()" GridLines="Both" HorizontalAlign="Center" DataKeyField="id" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<table style="width: 700px; background-color: #c8cacb;">
<tr>
<td style="width: 640px; text-align: left;">
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="12px" /><asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("ZS_leibiename") %>' Font-Size="12px"></asp:LinkButton></td>
</tr>
</table>//同事要我这里加上一个<table>试试
asp:DataList ID="DataList2" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" DataKeyField="id" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<table style="width: 158px;">
<tr onmouseover="this.style.backgroundColor='#c8cacb'" onmouseout="this.style.backgroundColor=''">
<td style="text-align: right;">
<asp:CheckBox ID="CheckBox2" runat="server" Font-Size="12px" ToolTip='<%# Eval("ZS_leibieid") %>' />
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("ZS_leibiename") %>' Font-Size="12px" CommandName="chakan"></asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
[解决办法]
好像是e.Item.Parent.Parent吧,你调一下吧,我这里网速太慢,刷新一次csdn要n久。