熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

DataGrid同時具有分頁和排序功能及注意點

2022-06-13   來源: ASP編程 
當DataGrid同時具有分頁和排序功能時應注意在重新綁定數據源時MyDataGridCurrentPageIndex=; 下面給實現以上功能的原碼也就不多綴了aspx中包含有DataGrid和控制其數據源變化的dropdownlist

  DataGrid代碼
   <asp:datagrid id=MyDataGrid runat=server BorderColor=#CCCCCC FontSize=% HorizontalAlign=Center
   AutoGenerateColumns=False OnDeleteCommand=MyDataGrid_Delete OnSortCommand=Sort_Grid OnPageIndexChanged=MyDataGrid_PageIndexChanged
   DataKeyField=ACC_NO PagerStylePosition=Bottom PagerStyleHorizontalAlign=Center PagerStyleMode=NextPrev
   PageSize= AllowSorting=True AllowPaging=True CellPadding= Width=%>
   <AlternatingItemStyle BackColor=#EEE></AlternatingItemStyle>
   <HeaderStyle FontBold=True Wrap=False ForeColor=White BackColor=#></HeaderStyle>
   <Columns>
   <asp:ButtonColumn Text= CommandName=Delete></asp:ButtonColumn>
   <asp:BoundColumn DataField=NO SortExpression=NO ReadOnly=True HeaderText=序號></asp:BoundColumn>
   <asp:BoundColumn DataField=ID SortExpression=ID HeaderText=ID></asp:BoundColumn>
   <asp:BoundColumn DataField=NAME SortExpression=NAME HeaderText=名稱></asp:BoundColumn>
   <asp:BoundColumn DataField=C_NAME SortExpression=C_NAME HeaderText=各科名稱></asp:BoundColumn>
   <asp:BoundColumn DataField=FLG SortExpression=FLG HeaderText=項目></asp:BoundColumn>
   </Columns>
   <PagerStyle NextPageText= PrevPageText=返回 HorizontalAlign=Center></PagerStyle>
   </asp:datagrid>
  
  
  dropdownlist代碼
  <asp:dropdownlist id=ddlWk Runat=server AutoPostBack=True Enabled=False>
   <asp:ListItem Value=>東京</asp:ListItem>
   <asp:ListItem Value=>九州</asp:ListItem>
   <asp:ListItem Value=>北海道</asp:ListItem>
   <asp:ListItem Value=>四國</asp:ListItem>
   </asp:dropdownlist>
  
  
  
  aspxcs文件代碼核心如下
  private void Page_Load(object sender SystemEventArgs e)
   {
   if(!IsPostBack)
   {
   Session[WP] =;
   ddlWk_getS();
   BindGrid();
   }
   }
   private void ddlWk_getS()
   {
   switch (Session[WP]ToString())
   {
   case :ddlWkSelectedIndex=;
   break;
   case :ddlWkSelectedIndex=;
   break;
   case :ddlWkSelectedIndex=;
   break;
   case :ddlWkSelectedIndex=;
   break;
   default:ddlWkSelectedIndex=;
   break;
   }
   }
   protected void BindGrid()
   {
   MyDataGridDataSource=GetData()Tables[vCO]DefaultView;
   MyDataGridDataBind();
   //COUNTText=MyDataGridColumnsCountToString();
   }
  
   /// <summary>
   /// 返回Data
   /// </summary>
   ///<returns></returns>
   private DataSet GetData()
   {
   string strConn=(String) ((NameValueCollection) ContextGetConfig(systemweb/database))[strConn];
   using (SqlConnection conn = new SqlConnection(strConn))
   {
   SqlCommand cmd = new SqlCommand(sp_Cconn);
   cmdCommandType=CommandTypeStoredProcedure;
   cmdParametersAdd(@placeSqlDbTypeVarChar);
   cmdParameters[@place]Value=Session[WP]ToString();
   connOpen();
  
   SqlDataAdapter da = new SqlDataAdapter();
   daSelectCommand=cmd;
   DataSet ds=new DataSet();
   daFill(dsvCO);
   CountText=ヒット+dsTables[vCO]RowsCountToString()+;
   return ds;
   }
  
   }
   /// <summary>
   ///從DataSet中除一
   /// </summary>
   /// <param name=sender></param>
   /// <param name=E></param>
   protected void MyDataGrid_Delete(Object sender DataGridCommandEventArgs E)
   {
   String strID=MyDataGridDataKeys[(int)EItemItemIndex]ToString();
   //刪除操作
   }
   /// <summary>
   /// 分頁操作
   /// </summary>
   /// <param name=sender></param>
   /// <param name=e></param>
   protected void MyDataGrid_PageIndexChanged(object source DataGridPageChangedEventArgs e)
   {
   MyDataGridCurrentPageIndex=eNewPageIndex;
   BindGrid();
   }
   /// <summary>
   /// 排序
   /// </summary>
   /// <param name=sender></param>
   /// <param name=e></param>
   protected void Sort_Grid(object sender DataGridSortCommandEventArgs e)
   {
   DataView dv= new DataView(GetData()Tables[vCO]);
   dvSort= eSortExpressionToString();
   MyDataGridDataSource=dv;
   MyDataGridDataBind();
   }
  
   #region Web override protected void OnInit(EventArgs e)
   {
   // //
   InitializeComponent();
   baseOnInit(e);
   }
  
   /// <summary> /// </summary>
   private void InitializeComponent()
   {
   thisddlWkSelectedIndexChanged += new SystemEventHandler(thisddlWk_SelectedIndexChanged);
   thisLoad += new SystemEventHandler(thisPage_Load);
  
   }
   #endregion
  
   private void ddlWk_SelectedIndexChanged(object sender SystemEventArgs e)
   {
   Session[WP]=ddlWkSelectedValue;
   MyDataGridCurrentPageIndex=;//沒有這一句當該頁碼超出其他數據源的范圍時會出錯
   BindGrid();
   ResponseWrite( <script language=javascript>nuframelocationreload();</script>);
  
   } 
  
From:http://tw.wingwit.com/Article/program/ASP/201311/21675.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.