Repeater控件

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Repeater : System.Web.UI.Page
{
    PagedDataSource PDS 
= new PagedDataSource();
    
private string ConnStr = ConfigurationManager.AppSettings["dbConnectionString"];
    
private string strsql = "";

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.Page.IsPostBack)
        {
            
this.bind();
        }

    }


    
private void bind()
    
{
        
int TotalCount = 0;//总记录数
        int TotalPage = 1//总页数
        
        SqlConnection conn 
= new SqlConnection(ConnStr);
        conn.Open();
        SqlDataAdapter da 
= new SqlDataAdapter("select CustomerID,CompanyName,ContactName,ContactTitle,Address from Customers order by CustomerID desc", conn);
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"Customers");
        DataView dv 
= ds.Tables[0].DefaultView;

        TotalCount 
= dv.Count;
        PDS.DataSource 
= dv;
        conn.Close();
        PDS.AllowPaging 
= true;
        PDS.PageSize 
= 20;
        
int CurPage;
        
if (Request.QueryString["Page"!= null)
        CurPage
=Convert.ToInt32(Request.QueryString["Page"]);
        
else
        CurPage
=1;

        
if (TotalCount == 0)
            TotalPage 
= 1;
        
else
{
            
if (TotalCount % PDS.PageSize == 0)
                TotalPage 
= TotalCount / PDS.PageSize;
            
else
                TotalPage 
= TotalCount / PDS.PageSize + 1;
        }


        PDS.CurrentPageIndex 
= CurPage-1;
        lblCurrentPage.Text 
= "" + TotalCount.ToString() + "条记录 当前页:" + CurPage.ToString() + "/" + TotalPage;

        lnkFrist.NavigateUrl 
= Request.CurrentExecutionFilePath + "?Page=1";
        
if (!PDS.IsFirstPage)
            lnkPrev.NavigateUrl 
= Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

        
if (!PDS.IsLastPage)
            lnkNext.NavigateUrl 
= Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
        lnkEnd.NavigateUrl 
= Request.CurrentExecutionFilePath + "?Page=" + TotalPage;

        Repeater1.DataSource
=PDS;
        Repeater1.DataBind();
    }


    
protected void btnDel_Click(object sender, EventArgs e)

{
        
string ID = "";

        
for (int i = 0; i < this.Repeater1.Items.Count; i++)
        
{
            CheckBox cbox 
= (CheckBox)this.Repeater1.Items[i].FindControl("chkItem");
            
if (cbox.Checked == true)
            
{
                
if (ID == "")
                
{
                    ID 
= "'"+((Label)this.Repeater1.Items[i].FindControl("lblID")).Text+"'";
                }

                
else
                
{
                    ID 
+= "," + "'" + ((Label)this.Repeater1.Items[i].FindControl("lblID")).Text + "'";
                }
 
            }

        }

        strsql 
= "delete from Customers where CustomerID in (" + ID + ")";
        
try
        
{
            SqlConnection conn 
= new SqlConnection(ConnStr);
            SqlCommand comm 
= new SqlCommand(strsql, conn);
            comm.Connection.Open();
            comm.ExecuteNonQuery();
            comm.Connection.Close();
            System.Web.HttpContext.Current.Response.Write(
"<script language='javascript'>alert('刪除成功!');</script>");
        }

        
catch (System.Data.SqlClient.SqlException E)
        
{
            
throw new Exception(E.Message);
        }

        
finally
        
{
            
if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        
this.bind();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值