今天做了一个管理文章的页面。
有两个datalist ,一个绑定文章信息,一个绑定文章对应的评论信息。
我先是在第一层datalist1中加了一个按纽事件,
在点击这个按钮之后就能实现对于文章对应的DataList2的绑定。
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
<ItemTemplate>
<asp:Label ID=lb_id runat=server Visible=false Text='<%# Eval("id") %>'></asp:Label>
<asp:Label ID=lb_title Font-Size=Large Font-Bold=true runat=server Text='<%# Eval("artTitle") %>'></asp:Label>
<asp:Label ID=lb_pubTime runat=server Text='<%# Eval("pubTime") %>'></asp:Label><br />
点击率:<asp:Label ID=lb_pointTimes runat=server Text='<%# Eval("pointTimes") %>'></asp:Label>
被顶次数:<asp:Label ID=lb_diggTimes runat=server Text='<%# Eval("diggTimes") %>'></asp:Label>
<asp:LinkButton ID=lbtn_view CssClass=btn CommandName="ViewContent" runat=server Text="查看内容"></asp:LinkButton>
<asp:LinkButton ID=lbtn_viewComment CommandName="viewComment" CommandArgument='<%# Eval("id") %>' CssClass=btn runat=server Text="查看评论"></asp:LinkButton>
<asp:LinkButton ID=lbtn_delete CommandName="artDelete" CommandArgument='<%# Eval("id") %>' CssClass=btn runat=server Text="删除"></asp:LinkButton>
<asp:Panel ID=pl_content runat=server Visible=false>
<asp:Label ID=lb_content runat=server Text='<%# Eval("artContent") %>'></asp:Label>
</asp:Panel>
<asp:Panel ID=pl_comment runat=server Visible=false>
<asp:Label ID=lb_msg2 runat=server ForeColor=red></asp:Label>
<asp:DataList ID="DataList2" runat="server" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<asp:Label ID=lb_comArtId runat=server Visible=false Text='<%# Eval("artId") %>'></asp:Label>
评论人:<asp:Label ID=lb_userName Text='<%# Eval("userName") %>' runat=server></asp:Label>
评论时间:<asp:Label ID=lb_comPubTime Text='<%# Eval("pubTime") %>' runat=server></asp:Label>
<br />
评论人联系方式:<asp:Label ID=lb_userContact Text='<%# Eval("userContact") %>' runat=server></asp:Label>
<br />
评论内容:
<asp:Label ID=lb_comContent Text='<%# Eval("commentContent") %>' runat=server ></asp:Label>
<asp:LinkButton ID=lbtn_comDelete runat=server CssClass=btn Text="删除" CommandName="comDelete" CommandArgument='<%# Eval("id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
对于这种的绑定还是很容易实现,方法如下:
if (e.CommandName == "viewComment")
{
Panel pl2 = (Panel)e.Item.FindControl("pl_comment");
if (pl2.Visible == false)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataList Dl2 = (DataList)e.Item.FindControl("DataList2");
Label lb2 = (Label)e.Item.FindControl("lb_msg2");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Dl2.Items.Count == 0 && lb2.Text == "")
{
string connStr = ConfigurationSettings.AppSettings["connectionString"];
DataControl myDataControl = new DataControl(connStr);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string sqlStr = "select * from commentInfo where artId='" + e.CommandArgument.ToString() + "' ";
DataTable table = myDataControl.GetTable(sqlStr);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (table.Rows.Count > 0)
{
lb2.Text = "";
}
else
{
lb2.Text = "该文章没有评论";
}
Dl2.DataSource = table;
Dl2.DataBind();
}
pl2.Visible = true;
}
else
{
pl2.Visible = false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
上面就基本实现了两个DATALIST的绑定了。
但是我现在想要,在子的datalist2中添一个删除按钮,点击以后进行数据库操作并再次对这个datalist进行绑定。
网上找了很久都没找到好的办法。
数据操作容易,主要是进行数据绑定时找不到这个子控件。
后来想到一个办法,就是Datalist2中用了一个Label来记录文章的ID,然后DataList1中也用了一个Label来记录他的主码id.方法不太好,我现在正想找有什么更简单性能更好的办法。。 希望和大家能交流、学习。
代码如下:
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "comDelete")
{
Label lb_artId = (Label)e.Item.FindControl("lb_comArtId");
string connStr = ConfigurationSettings.AppSettings["connectionString"];
DataControl myDataControl = new DataControl(connStr);
string sqlStr = "delete from commentInfo where id='" + e.CommandArgument.ToString() + "'";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
{
myDataControl.OpenConnection();
myDataControl.UpdateDataBase(sqlStr);
myDataControl.CloseConnection();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlStr = "select * from commentInfo where artId='" + lb_artId.Text + "'";
DataTable table = myDataControl.GetTable(sqlStr);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataListItem item in DataList1.Items)
{
Label lb_id = (Label)item.FindControl("lb_id");
if (lb_id.Text == lb_artId.Text)
{
DataList Dl2 = (DataList)item.FindControl("DataList2");
Label lb2 = (Label)item.FindControl("lb_msg2");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (table.Rows.Count > 0)
{
lb2.Text = "";
}
else
{
lb2.Text = "该文章没有评论";
}
Dl2.DataSource = table;
Dl2.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception ex)
{
lb_msg1.Text = "删除失败" + ex;
}
}
}
有两个datalist ,一个绑定文章信息,一个绑定文章对应的评论信息。
我先是在第一层datalist1中加了一个按纽事件,
在点击这个按钮之后就能实现对于文章对应的DataList2的绑定。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对于这种的绑定还是很容易实现,方法如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
上面就基本实现了两个DATALIST的绑定了。
但是我现在想要,在子的datalist2中添一个删除按钮,点击以后进行数据库操作并再次对这个datalist进行绑定。
网上找了很久都没找到好的办法。
数据操作容易,主要是进行数据绑定时找不到这个子控件。
后来想到一个办法,就是Datalist2中用了一个Label来记录文章的ID,然后DataList1中也用了一个Label来记录他的主码id.方法不太好,我现在正想找有什么更简单性能更好的办法。。 希望和大家能交流、学习。
代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)