动态生成的repeater中的按钮无法注册ItemCommand事件

aykkk 2019-08-27 05:23:58
想用repeater生成无限分类,参考网上资料后决定这样做:最顶级分类使用固定的repeater控件,下属的分类都用动态生成的repeater控件,生成的分类放在一个table中,一个类别一行,带一个按钮,按钮执行删除动作.写好之后发现,分类层次可以实现无限分类要求,但是只有最顶级分类,也就是那个固定repeater控件里的按钮可以执行动作,动态生成的repeater中绑定的按钮无法执行删除动作,而且一点击,除了最顶级分类外,其他各级分类都消失了.如何处理,请大佬帮忙?


代码如下:

前台html代码:

<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>


后台cs代码:

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;
}
}


repeater模板代码:

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);
}
...全文
90 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aykkk 2019-08-28
  • 打赏
  • 举报
回复
水平不够,无力解决次难题.难道真的要放弃?
极客诗人 2019-08-27
  • 打赏
  • 举报
回复
表示已经看不懂了
冰川711 2019-08-27
  • 打赏
  • 举报
回复
现在看到这种代码就感觉头大, 都19年了, 换一种思维 做 web程序吧,继续研究这个没有意义的,已经过时好久了
aykkk 2019-08-27
  • 打赏
  • 举报
回复
引用 1 楼 娃都会打酱油了 的回复:
Page_Load里面得有恢复动态创建的动作,然后注意设置id什么的得一致


该怎么做呢?如果我去掉if(!ispostback),发现点击下属分类的按钮正常执行,点击顶级分类的按钮却发生错误了,报表回发或回调参数无效
  • 打赏
  • 举报
回复
Page_Load里面得有恢复动态创建的动作,然后注意设置id什么的得一致

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧