欢迎光临
我们一直在努力

DataGrid代码(二)-.NET教程,数据库应用

建站超值云服务器,限时71元/月

 

using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
using system.drawing;
using system.drawing.design;

namespace my.ui.webcontrols
{
 #region summary
 /// <summary>
 /// datagrid 的摘要说明。
 /// </summary>
 [
 toolboxbitmap(typeof(my.ui.webcontrols.datagridtable), “my.ui.webcontrols.datagridtable.bmp”),
 ///defaultproperty(“text”),
 toolboxdata(“<{0}:datagridtable runat=server></{0}:datagridtable>”)]
 #endregion
 public class datagridtable : system.web.ui.webcontrols.datagrid,inamingcontainer,ipostbackeventhandler
 {

  #region variable 
  private linkbutton startpage =new linkbutton();
  private linkbutton prevpage =new linkbutton();
  private linkbutton nextpage =new linkbutton();
  private linkbutton endpage =new linkbutton();
  private string sstartpage=”startpage”;
  private string sprevpage=”prevpage”;
  private string snextpage=”nextpage”;
  private string sendpage=”endpage”;
  private dropdownlist numpage=new dropdownlist();
  private panel mypanel=new panel();
  //private int maxrecord=0;
  private string pagecss=”scrollbutton”;
  #endregion

  #region event
  //public event  datagridpagechangedeventhandler  pageindexchanged;
  #endregion

  #region property
  [bindable(true),
  category(“分页”),
  defaultvalue(“true”),description(“自定义分页”)]
  public bool isallowpaging
  {
   get
   {
    object obj=viewstate[“isallowpaging”];
    return ((null ==obj ) ? true : convert.toboolean( obj));

   }
   set
   {
    viewstate[“isallowpaging”]=value;
   }
  }
  [bindable(true),
  category(“分页”),
  description(“总记录数”)]
  public int maxrecord
  {
   get
   {
    object obj=viewstate[“maxrecord”];
    return ((null ==obj ) ? 0 : convert.toint32( obj));
   }
   set
   {
    viewstate[“maxrecord”]=value;
   }
  }
  [bindable(true),
  category(“翻页”),
  defaultvalue(“首页”),description(“回到首页”)]
  public string startpage
  {
   get
   {
    return sstartpage;
   }
   set
   {
    sstartpage=value;
   }
           
  }
  [bindable(true),
  category(“翻页”),
  defaultvalue(“尾页”),description(“回到尾页”)]
  public string endpage
  {
   get
   {
    return sendpage;
               
   }
   set
   {
    sendpage=value;
   }
           
  }
  [bindable(true),
  category(“翻页”),
  defaultvalue(“前页”),description(“上一页”)]
  public string prevpage
  {
   get
   {
    return sprevpage;
   }
   set
   {
    sprevpage=value;
   }
  }
  [bindable(true),
  category(“翻页”),
  defaultvalue(“后页”),description(“下一页”)]
  public string nextpage
  {
   get
   {
    return snextpage;
   }
   set
   {
    snextpage=value;
   }
  }
  [bindable(true),
  category(“翻页外观”),
  defaultvalue(“”),description(“按钮的css”)]
  public string pagecss
  {
   get
   {
    return pagecss;
   }
   set
   {
    pagecss=value;
   }
  }
  #endregion

  #region 构造
  /*
  public datagridtable():base(){
           
           
  }*/
  #endregion

  #region method

  protected override void oninit(eventargs e)
  {
           
   enableviewstate = true;

   //判断样式是否存在,不存在样式进行样式初始化
   if ((base.cssclass == null) || base.cssclass.trim().equals(“”))
   {
    cssclass = “tablegrid”;
   }
   if ((base.headerstyle.cssclass == null) || base.headerstyle.cssclass.trim().equals(“”))
   {
    headerstyle.cssclass = “gridheader”;
   }
   if ((base.footerstyle.cssclass == null) || base.footerstyle.cssclass.trim().equals(“”))
   {
    footerstyle.cssclass = “gridfooter”;
   }
   if ((base.pagerstyle.cssclass == null) || base.pagerstyle.cssclass.trim().equals(“”))
   {
    pagerstyle.cssclass = “gridpager”;
   }
   if ((base.itemstyle.cssclass == null) || base.itemstyle.cssclass.trim().equals(“”))
   {
    itemstyle.cssclass = “griditem”;
   }
   if ((base.alternatingitemstyle.cssclass == null) || base.alternatingitemstyle.cssclass.trim().equals(“”))
   {
    alternatingitemstyle.cssclass = “gridaltitem”;
   }
   if ((base.edititemstyle.cssclass == null) || base.edititemstyle.cssclass.trim().equals(“”))
   {
    edititemstyle.cssclass = “gridedititem”;
   }
   if ((base.selecteditemstyle.cssclass == null) || base.selecteditemstyle.cssclass.trim().equals(“”))
   {
    selecteditemstyle.cssclass = “gridselecteditem”;
   }
   //~样式初始化完毕

   base.oninit(e);
  }

       
  protected override void onprerender(eventargs e)
  {
   page.getpostbackeventreference(this);
  }
  protected override void onitemdatabound(datagriditemeventargs e)
  {
   base.onitemdatabound(e);
  }
  protected override void onitemcommand(datagridcommandeventargs e)
  {
   base.onitemcommand(e);
           
  }
  protected override bool onbubbleevent(object source,eventargs e)
  {
   return base.onbubbleevent( source,e);
  }
       

  /// <summary>
  /// 捕获回发事件
  /// </summary>
  /// <param name=”eventargument”>命令行</param>
  public void raisepostbackevent(string eventargument)
  {
   int newpageindex=0;
   if(pagerstyle.position.equals(pagerposition.bottom))
   {
    currentpageindex=convert.toint32( page.request.params[this.uniqueid+”:numpagebottom”])-1;
   }
   else
   {
    currentpageindex=convert.toint32( page.request.params[this.uniqueid+”:numpagetop”])-1;
   }
   if(“prevpage”.equals( eventargument))
   {
    newpageindex=currentpageindex-1;
   }
   else if(“nextpage”.equals( eventargument))
   {
    newpageindex=currentpageindex+1;
   }
   else if(eventargument.startswith(“endpage” ))
   {
    newpageindex=convert.toint32(eventargument.substring(7));
   }
   else if(“numpagetop”.equals( eventargument))
   {
    newpageindex=convert.toint32( page.request.params[this.uniqueid+”:numpagetop”])-1;
   }
   else if(“numpagebottom”.equals( eventargument))
   {
    newpageindex=convert.toint32( page.request.params[this.uniqueid+”:numpagebottom”])-1;
   }
   onpageindexchanged(new datagridpagechangedeventargs(this,newpageindex));
  }

  /// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name=”output”> 要写出到的 html 编写器 </param>
  ///

  protected override void render(htmltextwriter output)
  {
   if(isallowpaging)
   {
    allowpaging=true;
    pagerstyle.visible=false;
    mypanel.copybaseattributes(this);
    style.remove(“position”);
    style.remove(“z-index”);
    mypanel.renderbegintag(output);
    table table=new table();
    table.copybaseattributes(this);
    table.backcolor=(pagerstyle.backcolor.equals(color.empty))?this.backcolor:pagerstyle.backcolor;
    table.backimageurl=backimageurl;
    table.bordercolor=pagerstyle.bordercolor.isempty?bordercolor:pagerstyle.bordercolor;
    table.borderstyle=pagerstyle.borderstyle;
    table.borderwidth=unit.point(0);//(pagerstyle.borderwidth.isempty)?borderwidth:pagerstyle.borderwidth;
    table.horizontalalign=this.horizontalalign;
    table.width=width;
    table.cellpadding=cellpadding;
    table.cellspacing=cellspacing;
    table.gridlines=gridlines;
    table.font.size=fontunit.point(9);
    tablerow tr=new tablerow();
    table.controls.add(tr);
    tablecell td=new tablecell();
    startpage.cssclass=pagecss;
    startpage.text=sstartpage;
    prevpage.cssclass=pagecss;
    prevpage.text=sprevpage;
    nextpage.cssclass=pagecss;
    nextpage.text=snextpage;
    endpage.cssclass=pagecss;
    endpage.text=sendpage;
    numpage.cssclass=pagecss;
    tr.controls.add(td);
    td.attributes[“style”]=”border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none”;
    td.controls.add(new literalcontrol(“&nbsp;&nbsp;共<font color=red>”+maxrecord+”</font>条/每页<font color=red>”+pagesize+”</font>条”));
    td.controls.add(new literalcontrol(“&nbsp;当前第<font color=red>”+(currentpageindex+1)+”</font>页/共<font color=red>”+pagecount+”</font>页”));
    td.controls.add(new literalcontrol(“</td><td  style=\”border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none\”>”));
    if(currentpageindex!=0)
    {
     startpage.attributes[“href”]=”javascript:”+page.getpostbackeventreference(this,”startpage”);
     prevpage.attributes[“href”]=”javascript:”+page.getpostbackeventreference(this,”prevpage”);
     td.controls.add(startpage);
     td.controls.add(new literalcontrol(“&nbsp;”));
     td.controls.add(prevpage);
     td.controls.add(new literalcontrol(“&nbsp;”));
    }
    else
    {
     td.controls.add(new literalcontrol(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”));
    }
    if(currentpageindex<(pagecount-1))
    {
     nextpage.attributes[“href”]=”javascript:”+page.getpostbackeventreference(this,”nextpage”);
     endpage.attributes[“href”]=”javascript:”+page.getpostbackeventreference(this,”endpage”+(pagecount-1));
     td.controls.add(nextpage);
     td.controls.add(new literalcontrol(“&nbsp;”));
     td.controls.add(endpage);
     td.controls.add(new literalcontrol(“&nbsp;”));
    }
    else
    {
     td.controls.add(new literalcontrol(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”));
    }
    for(int i=0;i<pagecount;i++)
    {
     listitem list=new listitem((i+1).tostring());
     if(i==currentpageindex) list.selected=true;
     numpage.items.add(list);
    }
    td.controls.add(new literalcontrol(“</td><td align=right style=\”border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none\”>转到第”));
    td.controls.add(numpage);
    td.controls.add(new literalcontrol(“页”));
    if(pagerstyle.position==pagerposition.top||pagerstyle.position==pagerposition.topandbottom)
    {
     numpage.id=this.uniqueid+”:numpagetop”;
     numpage.attributes[“onchange”]=page.getpostbackeventreference(this,”numpagetop”);
     table.rendercontrol(output);
    }
    base.render(output);
               
    if(pagerstyle.position==pagerposition.bottom||pagerstyle.position==pagerposition.topandbottom)
    {
     numpage.id=this.uniqueid+”:numpagebottom”;
     numpage.attributes[“onchange”]=page.getpostbackeventreference(this,”numpagebottom”);
     table.rendercontrol(output);
    }
    mypanel.renderendtag(output);
   }
   else
   {
    base.render(output);
   }
   enableviewstate=false;
  }

  #endregion
 }

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » DataGrid代码(二)-.NET教程,数据库应用
分享到: 更多 (0)