正文

asp.net笔记(8)2008-04-01 20:43:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ghlat520/33799.html

分享到:

第九章    GridView, Repeater与DataList数据服务控件
                               1:GridView控件基本属性
                               2:让GridView有分页功能
                               3:GridView数据控件的七种字段类型
                               4:让GridView具有光棒效果
                               5:为GridView添加自动编号
                               6:把DataGrid的数据,导出到excel中
                               7:隐藏GridView的某一列
                               8:如何自定义分页栏
                              

                       功能:数据显示控件
                         1)如何让GridView显示数据库的表
       GridView1.DataSource=数据源
                            GridView1.DataBind();
                         2)EmptyDataText,EmptyDataRowStyle:没有任何数据时显示的文本,样式
                         3)GridLine:设置显示的网络线
                         4)ShowHead,ShowFooter:显示页头与页脚
                         5)AllowSorting:显示允许排序
                         6)AutoGenerateDeleteButton:是否自动产生删除按钮
                         7)AutoGenerateEditButton:是否自动产生编辑按钮
                         8)AutoGenerateSelButton:是否自动产生选择按钮
                         9)DataKeyNames:设置GridView主键的值
                        10)DataKeys:在设置了DataKeyNames之后,得到当前行主键的值
                               DataKeys[下标].value.tostring
                        11)Rows:得到gridview中行集合
                            A)GridView1.Rows[2].Cells[3].Text:得到第3行第4个单元格的值
                            B)Rows.Count:得到行的个数
                        12)AlternatingRowStyle:交替行的样式
                        13)FooterStyle:页脚样式
                        14)HeadStyle:头新式
                        15)AllowPaging:是否充许分页
                        16)PageSize:每一页的数据大小
                        17)PageIndex:当前行的下标
                        18)PageStyle:分页栏样式
                        19)RowStyle:数据行的样式
                        20):Width,Height:gridView的高和宽。一般要去掉,这样有利于排版

                                 让GridView有分页功能 (分页数据源不能是sqldataread)       
                             A)设置AllowPaging为true
                             B)设置PageSize大小
                             C)设置PageSetting的分页项
                               PageSettings:用来设置分页的项
                              1)Mode:设置分页导航条中分页的样式:
                                     11)NextPrevious:上一页按钮和下一页按钮。
                                     22)NextPreviousFirstLast:上一页按钮、下一页按钮、第一页按钮和最后一页按钮。
                                     33)Numeric:可直接访问页面的带编号的链接按钮。
                                     44)NumericFirstLast:带编号的链接按钮、第一个链接按钮和最后一个链接按钮
                                 2)FirstPageText:第一页按钮的文字。
                                 3)PreviousPageText:上一页按钮的文字。
                                 4)NextPageText:下一页按钮的文字。
                                 5)LastPageText:最后一页按钮的文字
                                6)PageButtonCount:当用户在mode里面选择Numeric时,所一页所显示的按钮个数
                                7)Position:用来设置分页文字的显示位置
                                8)PageImageUrl:用来设置一个图片来代替分页链接按钮
                             D)在PageIndexChanging里面写上代码
                                   this.GridView1.PageIndex = e.NewPageIndex;
                                   this.GridView1.DataSource =数据源
                                   this.GridView1.DataBind();//执行数据绑字代码
                             E)让用户实现转到功能:
                                   直接指定this.GridView1.PageIndex=转到的值
                                   然后进行数据的绑定
                             F)打开EnableSortingAndPagingCallBacks属性:
                                  该属性可以实现AjAx。异步更新

                                 GridView数据控件的七种字段类型
      1)数据绑定字段:BoundField。[手动绑定]
                                      1:AutoGenerateColumns:自动产生列,会自动根据数据源去创建列,但是有时候
                                        用户可能只需要其中的几列,这时应该取消该属性,并手动添加列
                                      2:DataField:用于绑定的列名
                                      3:HeadText:用于显示的新的列名
                                      4:NullDisplayText:空字段所显示的文本
                                      5:DataFormatString:字符串格式化功能
                                        此时一定要把HtmlEncode设为false生效
                                         {0:C}:显示货币类型
                                         {0:F}:显示固定小数点。{0:F2}显示两位小数
                                         {0:D}:显示为长日期格式                                            
                                    作用:
                                       A)修改字段标题
                                       B)格式化字符串

                                   2)ButtonField:按钮字段
                                     在GridView控件中显示自定义的按钮
                                       1:Text:显示在按钮上方的标题
                                       2)Type:按钮的样式。
                                       3)ImageUrl:按钮类型设置成图像时,所指定图像
                                       4)CommandName:用来设置按钮的命令名,以后好区别
                                         是点击了那个按钮
                                       4)单击按钮时,会触发RowCommand事件
                                           e.CommandArgument得到当前行的下标
                                           e.CommandName:得到单击按钮的命令名
                                      举例:
                                        1)有订单表,要求在gridview中添加一个按钮名为
                                          修改,执行打印两个按钮
                                        2)有一个商品表,放置一个按钮为放入购物蓝,和退回的按钮
                                          购物车暂时用ListBox来代替                                      

                     如何向GridView中添加客户端的JS  
                                      在一个RowDataBound事件里面写上代码:[是GridView绑定数据时,触发]
                                         if (e.Row.RowType == DataControlRowType.DataRow)
                                           {
                                             (e.Row.Cells[4].Controls[0] as LinkButton).Attributes.Add("onclick", "return confirm('确实要删除吗?');");
                                              其中e.Row代表每一个绑定的行
                                          }

                                     3)CommandFile命令按钮字段
                                       是显示预先定义好的按钮来进行select,edit,update,delete
                                       命令按钮本身就可以执行一些命令,而ButtonField的代码完全由
                                       用户自己写,所以CommandFile可以省很多代码
                                      1)有删除,选择,编辑三个已经做好的按钮
                                      2)如何删除数据                                  
                                         在RowDeleting事件里面写代码
                                         其中e.RowIndex得到删除的当前行的下标                                       
                                         如果要添加删除提示,请为GridView添加客户端脚本
                                         删除完毕之后,请重新绑定GridView到数据源
                                          if (this.GridView1.PageIndex != 0 && this.GridView1.Rows.Count == 1)
                                                 {
                                                    this.GridView1.PageIndex -= 1;
                                                 }
                                             与分页配合一起删除

                                      3)如何编辑数据:[一般不用]
                                             A:在RowEditing事件里面写上:[当用户单击了编辑按钮时]
                                                   this.GridView1.EditIndex = e.NewEditIndex;
                                                   GridDataBind();   //使GridView再次绑定
                                             B:在RowCancelingEdit事件写上:[当用户单击了取消按钮时]
                                                   this.GridView1.EditIndex = -1;
                                                   GridDataBind();
                                             C:在RowUpdating里面得到用户修改的新值
                                                  this.GridView1.Rows[e.RowIndex].Cells[下标].Controls[0] as TextBox
                                                  进行数据库操作
                                                  重新绑定
                                      4)如何选择数据:
                                           A:在SelectedIndexChanging里面写代码
                                             this.GridView1.Rows[e.NewSelectedIndex].Cells[1].Text
                                             得到用户所选中的行的字段的值
                                        举例:
                                           1:一个订单表。要求选择一个订单表的一条记录,
                                               同时显示中订单明细表的内容
                                     
                                   4)CheckBoxField复选框字段
                                      当把数据类型设为Bit类型时,会自动成为复选框

                                5)HyperLinkField超级链接 (用得比较多)
                       1:HeadText:列头文本
                                    2:DataNavigateUrlFields:用来生成Url查询字符串的字段名
                                    3)DataNavigateUrlFormatString:指定Url查询字符串
                                    4)DataTextFiled:用来显示超链接的字段,用来匹配DataTextFormatString
                                    4)DataTextFormatString:显示超链接的文本。比如:
                                              查看{0}的详细资料
                                    举例:
                                        1:一个订单表。要求选择一个订单表的一条记录,
                                               同时显示中订单明细表的内容

                                 6)ImageField:图像字段  
                                           1:HeadText:列头文本
                                           2:DataImageUrlField:用于显示图片的字段
                                7)TemplateFile:模板字段 (很灵活)
                                   当GridView所预定的几种字段都无法满足你的需求时,就可以使用
                                   模板字段。模板字段充许你加入各种你所需要的控件
                                 举例:
                                  1:在每一行的前面加上checkBox,以实现购物
                                     foreach (GridViewRow  gvr  in this.GridView1.Rows)
                                      {
                                        if ((gvr.Cells[0].FindControl("chk") as CheckBox).Checked)
                                       {
                                            s += gvr.Cells[1].Text;
                                       }          
                                     }
                                  2:把某一个字段,作为文本框类型并显示
                                    1)加一个模板列,在模板列中加入文本框
                                    2)在RowDataBound事件里面向该文本框赋值
                                  

                                         让GridView具有光棒效果
                                 if (e.Row.RowType == DataControlRowType.DataRow)
                                   {
                                      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='red'");
                                      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='yellow'");          
                                   }                                                                                                                
                                       为GridView添加自动编号
                                       1)添加一个绑定列
                                       2)在RowDataBound事件里面写上
                                         e.Row.Cells[0].Text = Convert.ToString(this.GridView1.PageIndex *this.GridView1.PageSize + e.Row.RowIndex+1);
                                           
                                       隐藏GridView的某一列
                                         在RowDataBound事件里面写上
                                         this.GridView1.Columns[0].Visible = false;

           如何自定义分页栏
                                       1)单右-->编辑模板-->PageTemplate
                                       2)拖拉一些LinkButton在里面。
                                          首页 上页  下页  末页   第几页/共几页
                                         并区分它们的CommandName值
                                       3)在RowCommand事件中处理这些按钮按下的事件
                                           e.CommandName == "a" 来判断按下了那个按钮
                                       4)在RowDataBound事件里面来访问分页栏中的标签
                                            if (e.Row.RowType == DataControlRowType.Pager)
                                                {
                                                    (e.Row.Cells[0].FindControl("lbld") as Label).Text = this.GridView1.PageIndex.ToString();            
                                                 }       
                                       把DataGrid的数据,导出到excel中
                                        1:在页面的类中添加              
                                               public override void VerifyRenderingInServerForm(Control control)
                                                   {
        
                                                   }
                                       2:写上如下代码:
                                            Response.ContentType = "application/vnd.ms-excel";     
                                            System.IO.StringWriter tw = new System.IO.StringWriter();
                                            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                                            this.GridView2.RenderControl(hw);//设置你要导出内容的控件,我这里是Repeater
                                            Response.Write(tw.ToString());
                                            Response.End();
                                                   
                                 以前使用GridView都是以表格的形式来显示的,有时候我们需要更自由的
                                 方式来展现数据。

                                            使用Repeater[一般很少用]
                                    Repeater为我们准备了很多的模板,但是这些模板只能够以进入源码视,手动来写
                                     1:Template:项目模板,
                                     2:SeparatorTemplate:分隔线模板
                                     3:AlternatingItemTemplate:交替项模板
                                     4:HeaderTemplate:头模板
                                     5:FooterTemplate:模板                                  
                                 通过<%#  DataBinder.Eval(Container.DataItem,"字段名")来得到
                                 绑定到Repeater的数据源字段的值                               
                                     举例:
                                           1:做一个在线购物选择商品模板
                                           2:做一个在线考试的界面
                   
                                      如何给Repeater分页                                    
            PagedDataSource pds = new PagedDataSource();
                                        pds.DataSource = DataOper.RunHasResult("select 工号,姓名,部门编号,图片 from 职工").DefaultView;
                                        pds.AllowPaging = true;
                                        pds.PageSize = 3;
                                        pds.CurrentPageIndex = CurrentPage;
                                        this.Repeater1.DataSource = pds;
                                        this.Repeater1.DataBind();

                                        DataList控件
                               DataList与Repeat控件的区别
                           DataList也有Repeat的那几个模板,此外还多了一个SelectedItemTemplate模板
                           DataList可以一行显示多笔数据,通过设置RepeatColumns=值
                           DataList直接在设计状态时,就可以进行模板的编辑,不像Repeat要写代码
                          
                             实现选择DataList控件中的条目后显示该条目的详细信息
                              1)增加ItemTemplate项目模板,并添加一个LinkButton,以些能进入SelectedItemTemplate模板
                              2)设计SelectedItemTemplate模板
                              3)在DataList的ItemCommand事件中,写上下列代码
                                 this.DataList1.SelectedIndex = e.Item.ItemIndex;
                                  重新绑定DataList
                               举例:
                                 举例:做一个在线购物选择商品模板,只显示图片和商品名,点击
                                       查看详细资料后,会得到该商品的详细资料
 

阅读(5919) | 评论(4)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册