GridView控件自定义分页详解_第1页
GridView控件自定义分页详解_第2页
GridView控件自定义分页详解_第3页
GridView控件自定义分页详解_第4页
GridView控件自定义分页详解_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.GridView控件自定义分页详解在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析! 1<asp:GridView ID="NewsGrid" runat="server" AutoGenerateColumns="False" AllowPaging="false" Width="100%"> 2  

2、0;         <Columns> 3                <asp:BoundField DataField="NewsId" HeaderText="新闻ID"/> 4        &

3、#160;       <asp:HyperLinkField DataNavigateUrlFields="NewsId" DataNavigateUrlFormatString="/Details.aspx?ID=0" 5                    DataTextField=

4、"Title" HeaderText="新闻标题"  ItemStyle-Width="70%"/> 6                <asp:BoundField DataField="PostTime" HeaderText="发布时间"/> 7    &

5、#160;           <asp:CommandField HeaderText="新闻管理" ShowCancelButton="False" ShowDeleteButton="True" 8                  &#

6、160; ShowEditButton="True"/> 9            </Columns>10        </asp:GridView>11        <div style=" height:16px; padding-top:5px; margin-

7、right:30px; float:right">12        <asp:HiddenField ID="CurrentPage" runat="server" Value="0"/>13        <asp:LinkButton ID="First" runat="server" CommandArgument=

8、"first" OnClick="PagerButton_Click">首 页</asp:LinkButton>14        <asp:LinkButton ID="Prev" runat="server" CommandArgument="prev" OnClick="PagerButton_Click">上一页</asp:LinkButton>15

9、0;       <asp:LinkButton ID="Next" runat="server" CommandArgument="next" OnClick="PagerButton_Click">下一页</asp:LinkButton>16        <asp:LinkButton ID="Last" runat="s

10、erver" CommandArgument="last" OnClick="PagerButton_Click">尾 页</asp:LinkButton>17        </div>  CS文件中的代码: 1        protected void PagerButton_Click(object sender, EventArgs e) 

11、;2         3            int pageIndx = Convert.ToInt32(CurrentPage.Value); 4            int totals = NewsManager.GetNews(0, pageSize).TotalRecord

12、s; 5            int pages = (totals % pageSize) = 0 ? (totals / pageSize) : (totals / pageSize + 1); 6            string arg = (LinkButton)sender).CommandArgument.ToString().ToLowe

13、r(); 7            switch (arg) 8             9                case "prev":10  &#

14、160;                 if (pageIndx > 0)11                    12          

15、              pageIndx -= 1;13                    14              

16、0;     break;15                case "next":16                    if (pageIndx < pages - 1)17 

17、;                   18                        pageIndx += 1;19     

18、60;              20                    break;21                ca

19、se "last":22                    pageIndx = pages - 1;23                    break;24   

20、;             default:25                    pageIndx = 0;26              &#

21、160;     break;27            28            CurrentPage.Value = pageIndx.ToString();29            NewsGrid.DataSourc

22、e = NewsManager.GetNews(pageIndx , pageSize).Entities;30            NewsGrid.DataBind();31          如何在GridView中增加效果protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  &

23、#160;         /将满足特定条件的行标为高亮        if (e.Row.RowType = DataControlRowType.DataRow)/判定当前的行是否属于datarow类型的行                    int money = Con

24、vert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MONEY");/取当前行的列值            if (money = 77)                e.Row.BackColor = Color.Red;    

25、60;       /string customer = (string)DataBinder.Eval(e.Row.DataItem, "CUSTOMER");            string customer = DataBinder.Eval(e.Row.DataItem, "CUSTOMER").ToString();     

26、;       if (customer = "sdf")                e.Row.BackColor = Color.Red;                /加入鼠标滑过的高亮效果  &#

27、160;     if (e.Row.RowType = DataControlRowType.DataRow)/判定当前的行是否属于datarow类型的行                    /当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色          

28、60; e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight=''");            /当鼠标离开的时候 将背景颜色还原的以前的颜色     

29、       e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight=''");                  /单击行改变行背景颜色    

30、0;   if (e.Row.RowType = DataControlRowType.DataRow)                  e.Row.Attributes.Add("onclick","this.style.backgroundColor='#99cc00' this.style.color='buttontext'this.style.

31、cursor='default'");        如何在GridView中一次性批量更新多行数据ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="10"     

32、0;                   Width="542px" AllowPaging="True" AllowSorting="True"                  

33、;       DataKeyNames="DB31_1,DB31_2" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging=&qu

34、ot;GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSorting="GridView1_Sorting" >                 &

35、#160;      <Columns>                            <asp:TemplateField HeaderText="序号">      

36、;                          <ItemTemplate>                     &#

37、160;          <%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>                        

38、60;       </ItemTemplate>                            </asp:TemplateField>        &#

39、160;                   <asp:TemplateField HeaderText="学历代码" SortExpression="DB1_1">                 

40、;               <EditItemTemplate>                               

41、0;    <%-<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DB1_1") %>'></asp:TextBox>-%>                     &

42、#160;         <asp:DropDownList ID ="ddlXL" runat="server"  DataValueField='<%# Bind("DB1_1") %>'></asp:DropDownList>             

43、0;                  </EditItemTemplate>                            &#

44、160;   <ItemTemplate>                                    <asp:Label ID="Label1" runat="serv

45、er" Text='<%# Bind("xueliText") %>'></asp:Label>                                </ItemTemplate> 

46、                           </asp:TemplateField>                   

47、60;        <asp:TemplateField HeaderText="学历名称" SortExpression="DB1_2">                            

48、    <EditItemTemplate>                                    <asp:TextBox ID="TextBox2" runat=

49、"server" Text='<%# Bind("DB1_2") %>'></asp:TextBox>                                </EditItemTempla

50、te>                                <ItemTemplate>              

51、60;                     <asp:Label ID="Label2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:Label>       

52、                         </ItemTemplate>                      &#

53、160;     </asp:TemplateField>                                         

54、0;        <asp:TemplateField HeaderText="操作" ShowHeader="False">                        <EditItemTemplate>  

55、                          <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"  &#

56、160;                              Text="更新"></asp:LinkButton>            

57、60;               <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"            

58、0;                   Text="取消"></asp:LinkButton>                        <

59、/EditItemTemplate>                        <ItemTemplate>                    

60、        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"                    

61、60;           Text="编辑" OnClientClick="return confirm('确认要编辑吗?');"></asp:LinkButton>                     

62、60;                              <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Del

63、ete"                                Text="删除" OnClientClick="return confirm('确认要删除吗?');"></asp:LinkButton&

64、gt;                            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Select&quo

65、t;                                Text="选择"></asp:LinkButton>           

66、;             </ItemTemplate>                        </asp:TemplateField>      

67、0;                </Columns>                       <AlternatingRowStyle BackColor="Aquamarine" /

68、>                    </asp:GridView>    / <summary>    / 绑定数据到GridView    / </summary>    private void GridViewBind() &

69、#160;          检索数据库        string strSql = "SELECT * FROM DB1"        得到数据集        this.GridView1.DataSource=conn.GetDs(strSql).Tables0.DefaultV

70、iew;        this.GridView1.DataBind();           / <summary>    / 编辑当前行    / </summary>    / <param name="sender"></param>    / <

71、;param name="e"></param>    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)            GridView1.EditIndex = e.NewEditIndex;        /当前编辑行背景色高亮 

72、0;      this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");        GridViewBind();        / <summary>    / 取消编辑状态    / </summary>  &#

73、160; / <param name="sender"></param>    / <param name="e"></param>    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)            GridVie

74、w1.EditIndex = -1;        GridViewBind();        / <summary>    / 删除记录过程    / </summary>    / <param name="sender"></param>    / <param na

75、me="e"></param>    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)            /得到单位编号        string rowToDelete = GridView1.DataKeyse.RowIndex.Values0

76、.ToString();        /转换为整数        /int ID=Convert.ToInt32(rowToDelete);        /从数据库中删除        string str = "DELETE FROM DB1 where DB1_1=" + "

77、9;" + rowToDelete + "'" + ""               try                conn.RunSql(str);        /重新绑定数据 &

78、#160;      GridViewBind();                catch (Exception ex)                Response.Write("数据库错误,错误原因:" + ex.Message);

79、0;       Response.End();                / <summary>    / 更新记录过程    / </summary>    / <param name="sender"></param>  

80、  / <param name="e"></param>    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)            string ID = GridView1.DataKeyse.RowIndex.Values0.ToString();    &#

81、160;   string DB1_1 = (TextBox)GridView1.Rowse.RowIndex.FindControl("TextBox1").Text;        /string DB1_2 = (TextBox)GridView1.Rowse.RowIndex.FindControl("TextBox2").Text;        string DB1_2 = (Drop

82、DownList)GridView1.Rowse.RowIndex.FindControl("ddlXL").SelectedItem.Text;            /判断表单项是否有空并给出提示信息        if (DB1_1 = "" | DB1_2 = "")        

83、0;           conn.Alert("请输入完整信息!", Page);            return;                      &#

84、160;     try               conn.BuilderEdit("select * from DB1 where DB1_1 ='" + ID + "'");        conn.dr"DB1_1" = DB1_1;   &#

85、160;    conn.dr"DB1_2" = DB1_2;        conn.BuilderEditClose();              catch (OracleException err)             

86、60;     if (err.Code.ToString() = "1")                conn.Alert("错误:已存在具有相同主键的记录", Page);            else    

87、            conn.Alert("错误:未能添加记录", Page);                Response.Write("<script language='javascript'>alert('数据已被保存!');</script>&q

88、uot;);        /返回浏览状态        GridView1.EditIndex = -1;        GridViewBind();        / <summary>    / 分页事件    / </summary>&

89、#160;   / <param name="sender"></param>    / <param name="e"></param>    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)           

90、 GridView1.PageIndex = e.NewPageIndex;        GridViewBind();        / <summary>    / 加入鼠标效果及为DropDownList绑定值    / </summary>    / <param name="sender"></param

91、>    / <param name="e"></param>    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)            /为DropDownList绑定值        if (DropDown

92、List)e.Row.FindControl("ddlXL") != null)                    DropDownList ddlXL = (DropDownList)e.Row.FindControl("ddlXL");            d

93、dlXL.Items.Clear();            ddlXL.Items.Add(new ListItem("博士", "1");            ddlXL.Items.Add(new ListItem("硕士", "2");    

94、60;       ddlXL.Items.Add(new ListItem("学士", "3");                /加入鼠标滑过的高亮效果        if (e.Row.RowType = DataControlRowType.DataRow)/判定当前的行是否属于d

95、atarow类型的行                    /当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgrou

96、ndColor;this.style.backgroundColor='yellow',this.style.fontWeight=''");            /当鼠标离开的时候 将背景颜色还原的以前的颜色            e.Row.Attributes.Add("onmouseout", "

97、this.style.backgroundColor=currentcolor,this.style.fontWeight=''");                /单击行改变行背景颜色        if (e.Row.RowType = DataControlRowType.DataRow)     

98、60;              e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00' this.style.color='buttontext'this.style.cursor='default'");         &

99、#160;  ASP.NET2.0下含有CheckBox的GridView删除选定记录实例<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="5"  2                   &

100、#160;     Width="726px" AllowPaging="True" AllowSorting="True" 3                          DataKeyNames="DB1_1,DB1_2

101、"  OnRowDeleting="GridView1_RowDeleting"   OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting" Height="279px" > 4       

102、;                  <Columns> 5                             <a

103、sp:TemplateField HeaderText="序号" > 6                                 <ItemTemplate> 7    &#

104、160;                            <%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%> 8     &#

105、160;                           </ItemTemplate> 9                  

106、60;          </asp:TemplateField>10                             <asp:TemplateField  HeaderText="

107、选择" >11                                 <ItemTemplate>12           

108、0;                         <asp:CheckBox ID="chkSelect" runat="server" ></asp:CheckBox>13          

109、;                       </ItemTemplate>14                        

110、;     </asp:TemplateField>15                          <asp:TemplateField HeaderText="操作" ShowHeader="False">16  &

111、#160;                           <ItemTemplate>17                             <asp:LinkButton ID="lbtnDelete" runat="server" CausesValidation="False" Co

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论