在DataList中套DataList 后,动态绑定第2层的datalist

今天做了一个管理文章的页面。
有两个datalist ,一个绑定文章信息,一个绑定文章对应的评论信息。
我先是在第一层datalist1中加了一个按纽事件,
在点击这个按钮之后就能实现对于文章对应的DataList2的绑定。
None.gif < asp:DataList ID = " DataList1 "  runat = " server "  OnItemCommand = " DataList1_ItemCommand "  OnItemDataBound = " DataList1_ItemDataBound "  BackColor = " White "  BorderColor = " #336666 "  BorderStyle = " Double "  BorderWidth = " 3px "  CellPadding = " 4 "  GridLines = " Horizontal " >
None.gif        
< ItemTemplate >
None.gif        
< asp:Label ID = lb_id runat = server Visible = false  Text = ' <%# Eval("id") %> ' ></ asp:Label >
None.gif            
< asp:Label ID = lb_title Font - Size = Large Font - Bold = true  runat = server Text = ' <%# Eval("artTitle") %> ' ></ asp:Label >   & nbsp;
None.gif            
< asp:Label ID = lb_pubTime runat = server Text = ' <%# Eval("pubTime") %> ' ></ asp:Label >< br  />
None.gif            点击率:
< asp:Label ID = lb_pointTimes runat = server Text = ' <%# Eval("pointTimes") %> ' ></ asp:Label >
None.gif            被顶次数:
< asp:Label ID = lb_diggTimes runat = server Text = ' <%# Eval("diggTimes") %> ' ></ asp:Label >
None.gif            
< asp:LinkButton ID = lbtn_view CssClass = btn CommandName = " ViewContent "  runat = server Text = " 查看内容 " ></ asp:LinkButton >
None.gif            
< asp:LinkButton ID = lbtn_viewComment CommandName = " viewComment "  CommandArgument = ' <%# Eval("id") %> '  CssClass = btn runat = server Text = " 查看评论 " ></ asp:LinkButton >
None.gif             
< asp:LinkButton ID = lbtn_delete CommandName = " artDelete "  CommandArgument = ' <%# Eval("id") %> '  CssClass = btn runat = server Text = " 删除 " ></ asp:LinkButton >
None.gif           
None.gif                 
< asp:Panel ID = pl_content runat = server Visible = false >
None.gif                
< asp:Label ID = lb_content runat = server Text = ' <%# Eval("artContent") %> ' ></ asp:Label >
None.gif            
</ asp:Panel >
None.gif            
< asp:Panel ID = pl_comment runat = server Visible = false >
None.gif               
None.gif      
< asp:Label ID = lb_msg2 runat = server ForeColor = red ></ asp:Label >
None.gif      
< asp:DataList ID = " DataList2 "  runat = " server "  OnItemCommand = " DataList2_ItemCommand " >
None.gif                
< ItemTemplate >
None.gif          
< asp:Label ID = lb_comArtId runat = server Visible = false  Text = ' <%# Eval("artId") %> ' ></ asp:Label >
None.gif                    评论人:
< asp:Label ID = lb_userName Text = ' <%# Eval("userName") %> '  runat = server ></ asp:Label >
None.gif                    评论时间:
< asp:Label ID = lb_comPubTime Text = ' <%# Eval("pubTime") %> '  runat = server ></ asp:Label >
None.gif                    
< br  />
None.gif                    评论人联系方式:
< asp:Label ID = lb_userContact Text = ' <%# Eval("userContact") %> '  runat = server ></ asp:Label >
None.gif                    
< br  />
None.gif                    评论内容:
None.gif                    
< asp:Label ID = lb_comContent Text = ' <%# Eval("commentContent") %> '  runat = server  ></ asp:Label >
None.gif                    
< asp:LinkButton ID = lbtn_comDelete runat = server CssClass = btn Text = " 删除 "  CommandName = " comDelete "  CommandArgument = ' <%# Eval("id") %> ' ></ asp:LinkButton >
None.gif                
</ ItemTemplate >
None.gif            
</ asp:DataList >
None.gif            
None.gif            
</ asp:Panel >
None.gif                                   
None.gif                                        
None.gif        
</ ItemTemplate >
None.gif                    
</ asp:DataList >

对于这种的绑定还是很容易实现,方法如下:
None.gif   if  (e.CommandName  ==   " viewComment " )
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Panel pl2 
= (Panel)e.Item.FindControl("pl_comment");
InBlock.gif            
if (pl2.Visible == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                DataList Dl2 
= (DataList)e.Item.FindControl("DataList2");
InBlock.gif                Label lb2 
= (Label)e.Item.FindControl("lb_msg2");
InBlock.gif
InBlock.gif                
if (Dl2.Items.Count == 0 && lb2.Text == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string connStr = ConfigurationSettings.AppSettings["connectionString"];
InBlock.gif                    DataControl myDataControl 
= new DataControl(connStr);
InBlock.gif
InBlock.gif                    
string sqlStr = "select * from commentInfo where artId='" + e.CommandArgument.ToString() + "";
InBlock.gif                    DataTable table 
= myDataControl.GetTable(sqlStr);
InBlock.gif
InBlock.gif                    
if (table.Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                      
InBlock.gif                        lb2.Text 
= "";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        lb2.Text 
= "该文章没有评论";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    Dl2.DataSource 
= table;
InBlock.gif                    Dl2.DataBind();
ExpandedSubBlockEnd.gif                }

InBlock.gif                pl2.Visible 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pl2.Visible 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

上面就基本实现了两个DATALIST的绑定了。

但是我现在想要,在子的datalist2中添一个删除按钮,点击以后进行数据库操作并再次对这个datalist进行绑定。
网上找了很久都没找到好的办法。
数据操作容易,主要是进行数据绑定时找不到这个子控件。
后来想到一个办法,就是Datalist2中用了一个Label来记录文章的ID,然后DataList1中也用了一个Label来记录他的主码id.方法不太好,我现在正想找有什么更简单性能更好的办法。。  希望和大家能交流、学习。
代码如下:
None.gif   protected   void  DataList2_ItemCommand( object  source, DataListCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {  
InBlock.gif        
if (e.CommandName == "comDelete")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
InBlock.gif            Label lb_artId 
= (Label)e.Item.FindControl("lb_comArtId");
InBlock.gif            
string connStr = ConfigurationSettings.AppSettings["connectionString"];
InBlock.gif            DataControl myDataControl 
= new DataControl(connStr);
InBlock.gif           
InBlock.gif            
string sqlStr = "delete from commentInfo where id='" + e.CommandArgument.ToString() + "'";
InBlock.gif
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myDataControl.OpenConnection();
InBlock.gif                myDataControl.UpdateDataBase(sqlStr);
InBlock.gif                myDataControl.CloseConnection();
InBlock.gif
InBlock.gif                sqlStr 
= "select * from commentInfo where artId='" + lb_artId.Text + "'";
InBlock.gif                DataTable table 
= myDataControl.GetTable(sqlStr);
InBlock.gif
InBlock.gif
InBlock.gif                
foreach (DataListItem item in DataList1.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Label lb_id 
= (Label)item.FindControl("lb_id");
InBlock.gif                    
if (lb_id.Text == lb_artId.Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DataList Dl2 
= (DataList)item.FindControl("DataList2");
InBlock.gif                        Label lb2 
= (Label)item.FindControl("lb_msg2");
InBlock.gif
InBlock.gif                        
if (table.Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                          
InBlock.gif                            lb2.Text 
= "";
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            lb2.Text 
= "该文章没有评论";
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        Dl2.DataSource 
= table;
InBlock.gif                        Dl2.DataBind();
InBlock.gif
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lb_msg1.Text 
= "删除失败" + ex;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/cainiao116/archive/2007/08/19/861490.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值