morenews2.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:6k
源码类别:
C#编程
开发平台:
Others
- using System;
- using System.Data;
- using System.Data.OleDb;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace www
- {
- public class MyCodeBehind : Page
- {
- public DataGrid MyList;
- public DropDownList DropDownList1;
- public DropDownList DropDownList2;
- public TextBox TextBox1;
- public LinkButton btnFirst;
- public LinkButton btnPrev;
- public LinkButton btnNext;
- public LinkButton btnLast;
- public Label lblCurrentPage;
- public Label lblPageCount;
- public Label lblRecordCount;
- public TextBox txtIndex;
- public int PageCount,RecordCount;
- public void SubmitBtn_Click(Object sender, EventArgs e)
- {
- {
- OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("candsql.mdb"));
- OleDbDataAdapter myCommand = new OleDbDataAdapter("select top 8 * from aa where " + DropDownList1.SelectedItem.Value + " like '%" + TextBox1.Text.ToString() + "%'"+" order by shijian desc", myConnection);
- DataSet ds = new DataSet();
- myCommand.Fill(ds, "tt");
- MyList.DataSource = ds.Tables["tt"].DefaultView;
- MyList.DataBind();
- }
- }
- private void Page_Load(Object sender, EventArgs e)
- {
- //确认页面不是回滚
- if(!IsPostBack)
- {
- //建立数据库连接
- OleDbConnection MyConnection =new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("candsql.mdb"));
- //创建OleDbDataAdapter对象,从bb表中选取数据
- OleDbDataAdapter myCommand=new OleDbDataAdapter("SELECT id,typename FROM bb ",MyConnection);
- DataSet ds= new DataSet();
- myCommand.Fill(ds,"bb");
- DropDownList2.DataSource = ds.Tables["bb"].DefaultView;
- //分别在DropDownList2的DataTextField属性和.DataValueField属性上绑定typename和id
- DropDownList2.DataTextField = "typename";
- DropDownList2.DataValueField = "id";
- DropDownList2.DataBind();
- DataBind();
- }
- DataBind();
- }
- DataView CreateDataSource()
- {
- OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("candsql.mdb"));
- OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from aa WHERE typeid="+ DropDownList2.SelectedItem.Value+""+" order by shijian desc", myConnection);
- DataSet ds = new DataSet();
- myCommand.Fill(ds, "aa");
- return ds.Tables["aa"].DefaultView;
- }
- void DataBind()
- {
- DataView source=CreateDataSource();
- if(!IsPostBack)
- {
- RecordCount=source.Count;
- PageCount=RecordCount/MyList.PageSize;
- if((RecordCount%MyList.PageSize)!=0) PageCount++;
- lblRecordCount.Text=RecordCount.ToString();
- lblPageCount.Text=PageCount.ToString();
- lblCurrentPage.Text="1";
- }
- MyList.DataSource = source;
- MyList.DataBind();
- }
- public void MyList_Page(Object sender, DataGridPageChangedEventArgs e)
- {
- //由内置页导航使用。CurrentPageIndex 已设置
- MyList.CurrentPageIndex = e.NewPageIndex;
- DataBind();
- }
- public void txtIndex_Changed(Object sender, EventArgs e)
- {
- btnFirst.Enabled=true;
- btnPrev.Enabled=true;
- btnNext.Enabled=true;
- btnLast.Enabled=true;
- int index=Int32.Parse(txtIndex.Text.ToString());
- PageCount=Int32.Parse(lblPageCount.Text.ToString());
- if(index>=1&&index<=PageCount)
- {
- MyList.CurrentPageIndex=index-1;
- DataBind();
- lblCurrentPage.Text=index.ToString();
- if(index==1)
- {
- btnFirst.Enabled=false;
- btnPrev.Enabled=false;
- }
- else if(index==PageCount)
- {
- btnLast.Enabled=false;
- btnNext.Enabled=false;
- }
- else
- { txtIndex.Text=""; }
- DataBind();
- }
- }
- public void PagerButtonClick(Object sender, CommandEventArgs e)
- {
- btnFirst.Enabled=true;
- btnPrev.Enabled=true;
- btnNext.Enabled=true;
- btnLast.Enabled=true;
- //由外部分页 UI 使用
- DataView source=CreateDataSource();
- RecordCount=source.Count;
- PageCount=RecordCount/MyList.PageSize;
- if((RecordCount%MyList.PageSize)!=0) PageCount++;
- String arg = e.CommandArgument.ToString();
- //PageCount=Int32.Parse(lblPageCount.Text.ToString());
- int pageindex=Int32.Parse(lblCurrentPage.Text.ToString())-1;
- switch(arg)
- {
- case "Next":
- if (pageindex < (PageCount - 1))
- pageindex ++;
- else
- pageindex=0;
- break;
- case "Prev":
- if(pageindex> (PageCount-1))
- pageindex=0;
- else if(pageindex> 0)
- pageindex--;
- break;
- case "Last":
- pageindex = (PageCount - 1);
- break;
- case "First":
- pageindex=0;
- break;
- }
- if(pageindex==0)
- {
- btnFirst.Enabled=false;
- btnPrev.Enabled=false;
- }
- else if(pageindex==PageCount-1)
- {
- btnLast.Enabled=false;
- btnNext.Enabled=false;
- }
- MyList.CurrentPageIndex=pageindex;
- DataBind();
- lblCurrentPage.Text=(MyList.CurrentPageIndex+1).ToString();
- }
- protected string FormatString(string str)
- {
- str=str.Replace(" "," ");
- str=str.Replace("<","<");
- str=str.Replace(">",">");
- str=str.Replace('n'.ToString(),"<br>");
- return str;
- }
- }
- }