遍历Repeater与ItemDataBound事件发现的几个问题。

1.如果用DataReader作为Repeater的数据源,ItemDataBound事件里通过FindControl找到控件
2.foreach遍历Repeater时,使用FindContrl需要判空,记得以前没有这个操作。
< table  class ="r_line"  cellpadding ="3"  cellspacing ="1"  width ="90%"  style ="margin-top:20px;" >
    
< tr  class ="r_title" >
        
< td  align ="center"  style ="width:100px;" > 编号 </ td >
        
< td  align ="center" > 产品名称 </ td >
    
</ tr >
    
< asp:Repeater  ID ="rptRecord"  runat ="server"  OnItemDataBound ="rptRecord_ItemDataBound" >
    
< ItemTemplate >
    
< tr  style ='background-color:<%#(Container.ItemIndex%2==0)?"#fff":"#eee"% > '>
        
< td >< asp:Label  ID ="lblPID"  runat ="server"  Text ="Label" ></ asp:Label ></ td >
        
< td >< asp:TextBox  ID ="txtPName"  runat ="server"  Width ="200px" ></ asp:TextBox ></ td >
    
</ tr >
    
</ ItemTemplate >
    
< FooterTemplate >    
    
< tr  class ="r_bg" >
    
< td  colspan ="2"  align ="center" >
        
< asp:Label  ID ="lblEmpty"  Text ="无记录.."  runat ="server"   Visible ='<%#bool.Parse((rptRecord.Items.Count==0).ToString())% > '> </ asp:Label >  
    
</ td >
    
</ tr >
    
</ FooterTemplate >   
    
</ asp:Repeater >
</ table >
protected   void  btnUpdate_Click( object  sender, EventArgs e)
{
    
foreach  (Control c  in  rptRecord.Controls)
    {
        
// 这样子行
        Label lblPID  =  c.FindControl( " lblPID " as  Label;
        
string  strPID  =   "" ;
        
if  (lblPID  !=   null ) // 必须加这个判断
            strPID  =  lblPID.Text;
        TextBox txtPName 
=  c.FindControl( " txtPName " as  TextBox;
        
string  strPName  =   "" ;
        
if  (txtPName  !=   null )
            strPName 
=  txtPName.Text;
        
// 下面的都不行,应该是因为这里会有null出现,不知何故。
        
// string strPName = ((TextBox)c.FindControl("txtPName")).Text;
        
// string strPName = (c.FindControl("txtPName") as TextBox).Text;

        
// 业务逻辑
    }
    Page.ClientScript.RegisterClientScriptBlock(
this .GetType(),  " alert " " <script>alert('更新成功!')</script> " false );
}
protected   void  rptRecord_ItemDataBound( object  sender, RepeaterItemEventArgs e)
{
    
if  (e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem)
    {
        DataRowView drv 
=  e.Item.DataItem  as  DataRowView;
        Label lblPID 
=  e.Item.FindControl( " lblPID " as  Label; // 如果是绑定的DataReader则找不到,不知何故。
        lblPID.Text  =  drv[ " PID " ].ToString();
        TextBox txtPName 
=  e.Item.FindControl( " txtPName " as  TextBox;
        txtPName.Text 
=  drv[ " pName " ].ToString();
    }
}

 

private   void  Bind()
{
    
string  Sqlstr  =   " select * from t_ProList " ;
    DataTable dt 
=   new  DataTable();
    
using  (SqlConnection conn  =   new  SqlConnection(ConfigurationManager.AppSettings[ " SQLCONNECTIONSTRING " ].ToString()))
    {
        SqlDataAdapter da 
=   new  SqlDataAdapter(Sqlstr, conn);
        conn.Open();
        da.Fill(dt);
        conn.Close();
    }
    rptRecord.DataSource 
=  dt; // 这里必须用DataTable才能动态绑定,绑定DataReader不行。
    rptRecord.DataBind();
}

 

转载于:https://www.cnblogs.com/greatverve/archive/2009/10/26/1589972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值