62,046
社区成员
发帖
与我相关
我的任务
分享
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate>
<table class="tablelist">
<thead>
<tr>
<th width="10%">编号</th>
<th width="10%">分类树</th>
<th width="40%">栏目名称</th>
<th width="10%">排序值</th>
<th width="10%">栏目类型</th>
<th width="15%">操作</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("classid") %></td>
<td><img src="images/dirfirst.gif" width="15" height="13"></td>
<td><%# Eval("classname") %></td>
<td><%# Eval("px") %></td>
<td><%# getlmlx(Eval("lmlx").ToString()) %></td>
<td>
<asp:Button ID="btndel" runat="server" Text="删除" CommandArgument='<%# Eval("classid") %>' CommandName="del" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DbUtility du = new DbUtility(cnstr, DbProviderType.OleDb);
string str = "Select classid,classname,px,lmlx From [Class] where fatherid='0' order by px desc, classid";
DataTable dt = du.ExecuteDataTable(str, null);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string id = e.CommandArgument.ToString();
switch (e.CommandName)
{
case "del":
Response.Write("<script>alert('OK1!删除成功" + id + ".');</script>");
break;
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
ListInnerCategory(e.Item, drv["classid"].ToString());
}
}
private int currentLevel = 0;
private void ListInnerCategory(RepeaterItem item, string ID)
{
string str = "Select count(*) From [Class] where fatherid=@cid ";
DbUtility du = new DbUtility(cnstr, DbProviderType.OleDb);
DbParameter[] pars = new DbParameter[]{
du.CreateDbParameter("@cid",ID)
};
int hasc =Convert.ToInt32(du.ExecuteScalar(str, pars));
if (hasc>0)
{
currentLevel++;
string str2 = "Select classid,classname,px,lmlx From [Class] where fatherid=@cid order by px desc, classid";
DbParameter[] pars2 = new DbParameter[]{
du.CreateDbParameter("@cid",ID)
};
DataTable dt = du.ExecuteDataTable(str2, pars2);
Repeater rep = new Repeater();
CategoryTemplate template = new CategoryTemplate(currentLevel);
rep.ItemTemplate = template;
rep.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound);
rep.ItemCommand += new RepeaterCommandEventHandler(rep_ItemCommand);
rep.DataSource = dt;
rep.DataBind();
item.Controls.Add(rep);
currentLevel--;
}
}
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableRow tr = (TableRow)e.Item.FindControl("tr2");
if (tr != null)
{
TableCell td1 = (TableCell)e.Item.FindControl("cid");
td1.Text = drv["classid"].ToString();
TableCell td2 = (TableCell)e.Item.FindControl("cpic");
td2.Text = @"<img src='images/dirsecond.gif' width='20' height='20'>";
TableCell td3 = (TableCell)e.Item.FindControl("cmc");
td3.Text = drv["classname"].ToString();
TableCell td4 = (TableCell)e.Item.FindControl("cpx");
td4.Text = drv["px"].ToString();
TableCell td5 = (TableCell)e.Item.FindControl("clx");
td5.Text = getlmlx(drv["lmlx"].ToString());
TableCell td6 = (TableCell)e.Item.FindControl("cop");
if (td6 != null)
{
Button btndel = (Button)e.Item.FindControl("btndel2");
btndel.CommandArgument = drv["classid"].ToString();
}
ListInnerCategory(e.Item, drv["classid"].ToString());
}
}
}
protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string id = e.CommandArgument.ToString();
switch (e.CommandName)
{
case "del2":
Response.Write("<script>alert('OK2!删除成功"+ id +".');</script>");
break;
}
}
private int currentLevel;
public CategoryTemplate(int level)
{
currentLevel = level;
}
public void InstantiateIn(Control container)
{
TableRow tr = new TableRow();
tr.ID = "tr2";
TableCell td1 = new TableCell();
td1.ID = "cid";
tr.Controls.Add(td1);
TableCell td2 = new TableCell();
td2.ID = "cpic";
tr.Controls.Add(td2);
TableCell td3 = new TableCell();
td3.ID = "cmc";
td3.Attributes.Add("style", "text-indent:" + (currentLevel * 20) + "px;");
tr.Controls.Add(td3);
TableCell td4 = new TableCell();
td4.ID = "cpx";
tr.Controls.Add(td4);
TableCell td5 = new TableCell();
td5.ID = "clx";
tr.Controls.Add(td5);
TableCell td6 = new TableCell();
td6.ID = "cop";
Button btndel = new Button();
btndel.ID = "btndel2";
btndel.Text = "删除";
btndel.CommandName = "del2";
td6.Controls.Add(btndel);
tr.Controls.Add(td6);
container.Controls.Add(tr);
}