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 ProductionInfo : System.Web.UI.Page
{    int CurrentPage;//当前页数
    int PageSize=4;   //每页条数
    int PageCount=9;  //总页数
    int RecordCount=9;//总条数
    SqlDataBase my_DataBase = new SqlDataBase();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            CurrentPage = 0;//当前页习惯设为0
            ViewState["PageIndex"] = 0;//页索引也设为0
            //计算总共有多少记录
            RecordCount = CalculateRecord();
            //计算总共有多少页
            if (RecordCount%PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = RecordCount / PageSize + 1;
            }            this.TotalLbl.Text = PageCount.ToString();//显示总页数
            ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session            this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
            bind();
        }
        
        PageSize = 4;//每页9条记录
    }    public string SubStr(string sString, int nLeng)
    {
        string sNewStr="";
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        else
        {
            sNewStr= sString.Substring(0, nLeng);
            sNewStr = sNewStr + "......";
        }
        return sNewStr;
    }    private void bind()//这个方法将数据库中产品内容读出来.显示在HyperLink上面.只显示100个字符..但是这样只能显示一条.因为我第页显示的是四条记录.我想这个显示的和datalist绑定的同步..
    {
        my_DataBase.SqlConnection_Open();
        SqlDataAdapter dar = new SqlDataAdapter("select a.fileContext,b.Production_Name,b.Production_id,b.type,b.Production_purpose from [File] as a,Production as b where b.Production_id=a.ID", my_DataBase.conn);
        dar.Fill(ds);
        HyperLink lb = (HyperLink)ProList.Items[0].FindControl("HyperLink2");        string str = "";
        str = ds.Tables[0].Rows[0][4].ToString();
        lb.Text = str;
        lb.Text = SubStr(lb.Text, 100);
        my_DataBase.connection_close();    }
    private int CalculateRecord()
    {
        try
        {
            int recordCount;
            my_DataBase.SqlConnection_Open();            string sql = "select count(*) as count from Production";
            SqlCommand cmd = new SqlCommand(sql,my_DataBase.conn);
            SqlDataReader sdr = cmd.ExecuteReader();            if (sdr.Read())
            {
                recordCount = Int32.Parse(sdr["count"].ToString());
            }
            else
            {
                recordCount = 0;
            }            sdr.Close();
            my_DataBase.connection_close();
            return recordCount;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    public void DataListBind()
    { 
  
        try
        {
            int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
            string sql = "select a.fileContext,b.Production_Name,b.Production_id,b.type,b.Production_purpose from [File] as a,Production as b where b.Production_id=a.ID ";
            
            my_DataBase.SqlConnection_Open();            SqlDataAdapter sda = new SqlDataAdapter(sql,my_DataBase.conn);
            sda.Fill(ds, StartIndex, PageSize, "Production");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
            
           
            string str = "";
            this.ProList.DataSource = ds.Tables["Production"].DefaultView;
            this.ProList.DataBind();
            this.PreviousLB.Enabled = true;
            this.NextLB.Enabled = true;
            if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
            if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
            this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
            my_DataBase.connection_close();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('The System It's Error!')</script>");
        }
    }
    public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
    {
        CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
        PageCount = (int)ViewState["PageCount"];//获得总页数
        string cmd = e.CommandName;        //判断cmd,以判定翻页方向
        try
        {
            switch (cmd)
            {
                case "prev"://上一页
                    if (CurrentPage > 0) CurrentPage--;
                    break;                case "next":
                    if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
                    break;                case "first"://第一页
                    CurrentPage = 0;
                    break;                case "end"://最后一页
                    CurrentPage = PageCount - 1;
                    break;                case "jump"://跳转到第几页
                    if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
                    {
                        return;
                    }
                    else
                    {
                        CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
                        break;
                    }            }
            ViewState["PageIndex"] = CurrentPage;//获得当前页            this.DataListBind();//重新将DataList绑定到数据库
        }
        catch (Exception)
        {
            Response.Write("<script language=javascript>alert('提示:数据格式错误!')</script>");
        }
    }
    }
  bind()//这个方法将数据库中产品内容读出来.显示在HyperLink上面.只显示100个字符..但是这样只能显示一条.因为我第页显示的是四条记录.我想这个显示的和datalist绑定的同步..
哪位大哥帮小弟写一下.刚入门.别见笑..解决后立马结帐.谢谢了...

解决方案 »

  1.   

    我的这个页面主要显示产品详细信息..每条信息包括.产品图片,产品型号,产品名称.和产品内容(产品内容是显示100个字符出来.点击的时候才看详细信息)
    产品都可以显示出来了.问题是产品内容只有一条可以显示.其它都没有.我的方法在private   void   bind()里面(page_load往下数第三个)...
    我现在不知道产品内容怎么每条都可以显示出来.和datalist绑定的方法(public   void   DataListBind() )放在一起的话会报错.我只会取一条.全部显示出来不会..但是必须要和datalist绑定的一起才行呀.
      

  2.   

    目的就是要datalist里的hayperlin显示a.fileContext.因为a.fileContext内容很多.我只显示100个字符.后面为"......"
      

  3.   

    public static string FixLenB(string strContent,int sLen)
    {
    //超过规定长度加上“…” byte[] s1 = System.Text.Encoding.Default.GetBytes(strContent);
    int tLen=sLen+2; //规定长度加上“…”的总长度
    //若转换为字节的字符串的长度小于指定的长度加上“...”的总长度则执行
    if(s1.Length<=tLen)
    {
    return strContent;
    }
    else
    {
    strContent=System.Text.Encoding.Default.GetString(s1,0,sLen)+"…";;
    return strContent;
    }
    }
      

  4.   

    lovehongyun您好!
    你这方法是取字符数.但是我不知道怎么在datalist绑定的时候与hyperlink绑定
    这个方法里面:
      public   void   DataListBind() 
            {   
        
                    try 
                    { 
                            int   StartIndex   =   CurrentPage   *   PageSize;//设定导入的起终地址 
                            string   sql   =   "select   a.fileContext,b.Production_Name,b.Production_id,b.type,b.Production_purpose   from   [File]   as   a,Production   as   b   where   b.Production_id=a.ID   "; 
                            
                            my_DataBase.SqlConnection_Open();                         SqlDataAdapter   sda   =   new   SqlDataAdapter(sql,my_DataBase.conn); 
                            sda.Fill(ds,   StartIndex,   PageSize,   "Production");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet   ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName 
                            
                          
                            string   str   =   ""; 
                            this.ProList.DataSource   =   ds.Tables["Production"].DefaultView; 
                            this.ProList.DataBind(); 
                            this.PreviousLB.Enabled   =   true; 
                            this.NextLB.Enabled   =   true; 
                            if   (CurrentPage   ==   (PageCount   -   1))   this.NextLB.Enabled   =   false;//当为最后一页时,下一页链接按钮不可用 
                            if   (CurrentPage   ==   0)   this.PreviousLB.Enabled   =   false;//当为第一页时,上一页按钮不可用 
                            this.CurrentLbl.Text   =   (CurrentPage   +   1).ToString();//当前页数 
                            my_DataBase.connection_close(); 
                    } 
                    catch   (Exception   ex) 
                    { 
                            Response.Write(" <script> alert('The   System   It's   Error!') </script> "); 
                    } 
            } 在 this.ProList.DataSource   =   ds.Tables["Production"].DefaultView; 
                            this.ProList.DataBind(); 
    与数据库绑定的时候不知道怎么把这个hyperlink也绑定了.如果直接在前台用Data.Eval绑的话.就全都出来了.嘿嘿.
      

  5.   

    我在绑定的时候这样写不对
     public void DataListBind()
        { 
      
            try
            {
                int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
                string sql = "select a.fileContext,b.Production_Name,b.Production_id,b.type,b.Production_purpose from [File] as a,Production as b where b.Production_id=a.ID ";
                
                my_DataBase.SqlConnection_Open();            SqlDataAdapter sda = new SqlDataAdapter(sql,my_DataBase.conn);
                sda.Fill(ds, StartIndex, PageSize, "Production");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
                string str = "";
                string  str1 = "";
                str= FixLenB(ds.Tables[0].Rows[0][4].ToString(), 100);
                str1=ds.Tables[0].Rows[0][4].ToString();
                str1  = str;
                this.ProList.DataSource = ds.Tables["Production"].DefaultView;
                this.ProList.DataBind();
                
                this.PreviousLB.Enabled = true;
                this.NextLB.Enabled = true;
                if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
                if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
                this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
                my_DataBase.connection_close();
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('The System It's Error!')</script>");
            }
        }
    其中把找到出来的内容ds.tabels[0].rows[0][4].tostring()调用了
    public static string FixLenB(string strContent,int sLen)
            {
                //超过规定长度加上“…”            byte[] s1 = System.Text.Encoding.Default.GetBytes(strContent);
                int tLen=sLen+2; //规定长度加上“…”的总长度
                //若转换为字节的字符串的长度小于指定的长度加上“...”的总长度则执行
                if(s1.Length<=tLen)
                {
                    return strContent;
                }
                else
                {
                    strContent=System.Text.Encoding.Default.GetString(s1,0,sLen)+"…";;
                    return strContent;
                }
            }
    但没效.因为我是在页面的时候用data.eval绑定这个hayperlink的
      

  6.   

    目的就是要datalist里的hayperlin显示a.fileContext.因为a.fileContext内容很多.我只显示100个字符.后面为"......"==>
    放在datalist中就可以
    <itemtemplate><a href="..."><%=FixLenB(Eval("字段").ToString(),100)%></a>
      

  7.   

    慕白兄  我按你的方法在前台页面加上了
     <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="Small" Height="80px" Width="483px" Text='<% FixLenB(Eval("Production_purpose").ToString(),100)%>'  NavigateUrl='<%# "DataListInfo.aspx?UID=" + DataBinder.Eval(Container.DataItem,"Production_id") %>'>[HyperLink2]</asp:HyperLink>
    这样的话显示时内容没了..
    Text='<% FixLenB(Eval("Production_purpose").ToString(),100)%>' 可以访问后台的方法呀..
    再指点一下..感激不尽