zoukankan      html  css  js  c++  java
  • 遍历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="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();
    }
  • 相关阅读:
    [php learn] php 从头開始学习1
    Qt实现Windows远程控制
    [core java学习笔记][第十一章异常断言日志调试]
    网络安全-安全散列函数,信息摘要SHA-1,MD5原理
    Cardboard虚拟现实开发初步(二)
    C#中Stack&lt;T&gt;类的使用及部分成员函数的源代码分析
    编程算法
    读取spring配置文件的方法(spring读取资源文件)
    Spring中的事务管理详解
    配置spring事务管理的几种方式(声明式事务)
  • 原文地址:https://www.cnblogs.com/greatverve/p/1589972.html
Copyright © 2011-2022 走看看