http://community.csdn.net/Expert/topic/3593/3593845.xml?temp=.6712915

解决方案 »

  1.   


    /// <summary>
    /// 绑定数据
    /// </summary>
    public override void DataBind()
    {
    this.bindpageInfo();
    if(this.PagerStyle == PagerMode.NextPager)
    {
    bool isValidPage = CurrPage >1;
    bool canMoveForward = (CurrPage<=PageCount-1);
    ((LinkButton)FindControl("First")).Enabled = isValidPage;
    ((LinkButton)FindControl("pre")).Enabled = isValidPage;
    ((LinkButton)FindControl("next")).Enabled = canMoveForward;
    ((LinkButton)FindControl("last")).Enabled = canMoveForward;
    }
    else
    {
    DropDownList dp = (DropDownList)FindControl("droplist");
    dp.Items.Clear();
    for(int i=1;i<this.PageCount+1;i++)
    {
    ListItem li = new ListItem(i.ToString(),i.ToString());
    dp.Items.Add(li);
    }
    dp.SelectedIndex = CurrPage-1;
    }
    if(this.BindTableName==null||this.BindTableName==null||this.SortFeild==null)
    return;
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[AppSetting].ToString());
    SqlDataAdapter da;
    if(this.PageCount==this.CurrPage&&this.ModRecord!=0)
     da = new SqlDataAdapter("select top "+ModRecord.ToString()+" *  from "+BindTableName.ToString()+"  order by "+SortFeild.ToString()+" asc",conn);
    else
     da = new SqlDataAdapter("select top "+PageSize.ToString()+" * from (select top "+(CurrPage*PageSize).ToString()+" * from "+BindTableName+" order by "+SortFeild+" desc) a order by "+SortFeild+" asc",conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    Control _control = Page.FindControl(ControlsToValidate);
    if(_control is BaseDataList)
    {
    BaseDataList basedatalist  = (BaseDataList)_control;
    basedatalist.DataSource = ds;
    basedatalist.DataBind();
    }
    ((Label)FindControl("ShowMessage")).Text =  "总记录数:<font color=blue>"+this.RecordCount.ToString()+"</font>,每页条数:<font color=blue>"+this.PageSize.ToString()+"</font>,总页数:<font color=blue>"+this.PageCount.ToString()+"</font>,当前页:<font color=blue>"+this.CurrPage.ToString()+"</font>";
    } private void bindpageInfo()
    {
    if(this.BindTableName==null||this.BindTableName==null||this.SortFeild==null)
    return;
    try
    {
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings[AppSetting].ToString());
    SqlCommand da = new SqlCommand("select count(*) from "+this.BindTableName,conn);
    conn.Open();
    int recordCount = (int)da.ExecuteScalar();
    this.RecordCount = recordCount;
    conn.Close();
    if(recordCount%this.PageSize==0)
    {
    this.PageCount = recordCount/this.PageSize;
    this.ModRecord = 0;
    }
    else
    {
    this.PageCount = recordCount/this.PageSize + 1;
    this.ModRecord = recordCount % this.PageSize;
    }
    }
    catch(SqlException e)
    {
    throw e;
    }
    }
    }}
      

  2.   

    using System;
    using System.Web;
    using System.IO;
    using System.Data;
    using System.Collections;
    using System.Text;
    using JLTender;
    namespace myfile
    {
    public class CFile : IHttpHandler
    {
    public CFile()
    {}
    #region IHttpHandler 成员 public void ProcessRequest(HttpContext context)
    {
                HttpRequest Request = context.Request;
    HttpResponse Response = context.Response; int id = Convert.ToInt32 (Request.Params ["id"]);
    int type = Convert.ToInt32 (Request.Params ["type"]); if (id == 0)
    {
    throw new ArgumentException ();
    } JLTender.Business.Account.User user = new JLTender.Business.Account.User ();
    DataRow dr = user.GetImg (id); if (dr == null)
    {
    throw new Exception ("未找到相关信息!");
    } string filePath = "\\";
                string oldFileName = dr ["oldimgname"].ToString ().Trim ();
    string ext = Path.GetExtension (oldFileName);
                
    string [] imgs = {".gif",".jpg",".jpeg",".png",".bmp"};
    ArrayList list = new ArrayList ();
    list.AddRange (imgs); if (list.Contains (ext.ToLower ()))
    {
    filePath += "_uploadpic\\";
    }
    else
    {
    filePath += "_uploadfile\\";
    } FileStream fs = null;
    try
    {
    fs = File.OpenRead (context.Server.MapPath (filePath) + dr ["newimgname"].ToString ().Trim ());
    byte[] buffer = new byte [fs.Length];
    fs.Read (buffer, 0, (int)buffer.Length);
                    
    Response.Clear ();
    if (type == 1)
    {
    Response.ContentType = "image/*";
    }
    else
    {
    Response.ContentType = "application/octet-stream";
    Response.AddHeader ("Content-Disposition","attachment;filename=" + oldFileName);
    }
    Response.BinaryWrite (buffer);
    Response.End ();
    }
    catch
    {}
    finally
    {
                    fs.Close ();                
    }            
    } public bool IsReusable
    {
    get
    {
    // TODO:  添加 CFile.IsReusable getter 实现
    return true;
    }
    } #endregion
    } public class CImg : IHttpHandler
    {
    public CImg ()
    {} #region IHttpHandler 成员 public void ProcessRequest(HttpContext context)
    {
    string querystr = context.Request.ServerVariables ["QUERY_STRING"].ToString ();
    StringBuilder sb = new StringBuilder ();
    sb.Append ("<html><head><title>图片浏览</title></head><body>");
    sb.AppendFormat ("<img src='ShowFile.ashx?{0}'>", querystr);
    sb.Append ("</body></html>");
                context.Response.Write (sb.ToString ());
    context.Response.End ();
    } public bool IsReusable
    {
    get
    {
    return true;
    }
    } #endregion }}