DataList嵌套
前台代码:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="100%" CssClass="subtable" ItemStyle-CssClass="subcol" CellSpacing="1" HeaderStyle-CssClass="subcoltitle" DataKeyField="TypeId" OnItemDataBound="DataList1_ItemDataBound1" OnItemCommand="DataList1_ItemCommand" OnItemCreated="DataList1_ItemCreated" OnDataBinding="DataList1_DataBinding">
<ItemTemplate>
<asp:Label ID="TypeID" runat="server" Text='<%# Eval("TypeID") %>'></asp:Label></td>
<td class="subcol" style="width:15%;"><asp:Label ID="Label2" runat="server" Text='<%# Eval("TypeName") %>'></asp:Label></td>
<td class="subcol" style="width:5%;">
<asp:DataList ID="dlshowUsers" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
</td>
<td class="subcol" style="width:25%;"><asp:LinkButton ID="LinkEdit" runat="server" CommandName="FrankEdit" Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkDel" runat="server" CommandName="FrankDel" Text="删除"></asp:LinkButton>
</ItemTemplate>
<HeaderTemplate >
类别编号</td>
<td class="subcoltitle" style="width:20%;">类别名称</td>
<td class="subcoltitle" style="width:40%;">客服解答</td>
<td class="subcoltitle" style="width:25%;">操作
</HeaderTemplate>
<ItemStyle CssClass="subcol" />
<AlternatingItemStyle BackColor="#ded9f9" />
<AlternatingItemTemplate >
<asp:Label ID="TypeID" runat="server" Text='<%# Eval("TypeID") %>'></asp:Label></td>
<td style="width:15%; background-color:#ded9f9; text-align:center;"><asp:Label ID="Label2" runat="server" Text='<%# Eval("TypeName") %>'></asp:Label></td>
<td style="width:5%;background-color:#ded9f9; text-align:center;">
<asp:DataList ID="dlshowUsers" runat="server" Width="99%">
<ItemTemplate><div style="text-align:center;">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label></div>
</ItemTemplate>
</asp:DataList>
</td>
<td style="width:25%;background-color:#ded9f9; text-align:center;"><asp:LinkButton ID="LinkEdit" runat="server" CommandName="FrankEdit" Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkDel" runat="server" CommandName="FrankDel" Text="删除"></asp:LinkButton>
</AlternatingItemTemplate>
<HeaderStyle CssClass="subcoltitle" />
</asp:DataList>
后台代码:
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dlDir2 = (DataList)e.Item.FindControl("dlshowUsers");
string aa = DataList1.DataKeys[e.Item.ItemIndex].ToString().Trim();
dlDir2.DataSource = H_Faq.Base.dbODSGetSDR("select userType.qTypeID,userType.UserName ,Qtype.TypeID from UserType,QType where UserType.QTypeID = QType.TypeID and Qtype.TypeId=" + aa);
dlDir2.DataBind();
}
}
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
case ListItemType.SelectedItem:
((LinkButton)(e.Item.FindControl("LinkDel"))).Attributes.Add(
"onclick", " return confirm('你确认要删除');");
break;
}
}
/// <summary>
/// Datalist事件驱动。
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
for (int i = 0; i < cblUsers.Items.Count; i++)
{
cblUsers.Items[i].Selected = false;
}
//Response.Write("<script>alert('"+e.CommandName+"')</script>");
if (e.CommandName == "FrankEdit")
{
lblTypeId.Text = ((Label)(e.Item.FindControl("TypeID"))).Text;
txtTypeName.Text = H_Faq.Base.dbExecuteSql4Values("select TypeName from QType where TypeId=" + lblTypeId.Text);
SqlDataReader sdr = H_Faq.Base.dbODSGetSDR("select UserName from UserType where QTypeId="+lblTypeId.Text);
while (sdr.Read())
{
for (int i = 0; i < cblUsers.Items.Count; i++)
{
if (cblUsers.Items[i].Text==sdr[0].ToString())
{
cblUsers.Items[i].Selected = true;
}
}
}
btnnew.Visible = false;
btnReturn.Visible = true;
btnSave.Visible = true;
}
if (e.CommandName == "FrankDel")
{
string a = ((Label)(e.Item.FindControl("TypeID"))).Text;
string [] sqls=new string[2];
sqls[0] = "delete from UserType where QTypeId=" + a;
sqls[1] = "delete from QType where TypeId=" + a;
H_Faq.Base.dbExecuteSqls(sqls);
DataList1.DataBind();
}
}