分享

gridview的事件大全

 CrazyProgrammer 2010-08-17
DataKeyNames属性
很多时候我们需要在GridView的RowCommand之类的事件中需要获取当前行的一些关联性的数据值。但这些数据值又没有直接体现在GridView的列中。这个时候该怎么办呢?
有的同学喜欢用隐藏列的方式,把需要使用但不显示的字段绑定到此列上,同时设置列宽为0或不显示,使用时可以用常规的取某行某列的方式来获取数据。
但是在Framework 2.0中,我们可以采用DataKeyNames的方式来获取此类数据。
Grup 为我们想使用但不需要显示的列。(如果有多个字段,使用逗号分开)
.aspx
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Grup" OnRowCommand="GridView1_RowCommand" >
.cs
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        // 获取当前行索引 
        int index = Convert.ToInt32(e.CommandArgument);
        // 取出当前行数据键值对象中的值 
        string strGrup = ((GridView)sender).DataKeys[index].Values["Grup"].ToString();  
    }
PageIndexChanged
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之后发生。此事件通常用于以下情形:在用户定位到该控件中的另一页之后,您需要执行某项任务。
//翻页
        protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            string recordNumber = "0";
            DataTable dt = GetSearchResult(AspNetPager1.CurrentPageIndex.ToString(), ref recordNumber); 
            if (dt != null && dt.Rows.Count > 0)
            {
                GridDataBind(dt, recordNumber);
            }
        }
PageIndexChanging
在单击某一页导航按钮时,但在 GridView 控件处理分页操作之前发生。此事件通常用于取消分页操作。
.aspx  <PagerTemplate> 与<columns>同级别
<PagerTemplate>
                        <table width="100%">
                            <tr>
                                <td style="text-align: right">
                                    第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1   %>' />页
                                    共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount   %>' />页
                                    <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
                                        CommandName="Page" Text="首页" />
                                    <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
                                        CommandName="Page" Text="上一页" />
                                    <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
                                        CommandName="Page" Text="下一页" />
                                    <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
                                        CommandName="Page" Text="尾页" />
                                    <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1   %>' />
                                    <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
                                        CommandName="Page" Text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
                                </td>
                            </tr>
                        </table>
                    </PagerTemplate>
.cs
        protected void gvShippingApply_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {
            GridView theGrid = sender as GridView;   // refer to the GridView
            int newPageIndex = 0;
            if (-2 == e.NewPageIndex)
            { // when click the "GO" Button
                TextBox txtNewPageIndex = null;
                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
                if (null != pagerRow)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;    // refer to the TextBox with the NewPageIndex value
                }
                if (null != txtNewPageIndex)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                }
            }
            else
            {   // when click the first, last, previous and next Button
                newPageIndex = e.NewPageIndex;
            }
            // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
            // specify the NewPageIndex
            theGrid.PageIndex = newPageIndex;
            this.BindData();//重新绑定数据
        }
RowCancelingEdit
在单击某一行的“取消”按钮时,但在 GridView 控件退出编辑模式之前发生。此事件通常用于停止取消操作。
RowCommand
当单击 GridView 控件中的按钮时发生。此事件通常用于在控件中单击按钮时执行某项任务。
RowCreated
当在 GridView 控件中创建新行时发生。此事件通常用于在创建行时修改行的内容。
RowDataBound
在 GridView 控件中将数据行绑定到数据时发生。此事件通常用于在行绑定到数据时修改行的内容。
RowDeleted
在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之后发生。此事件通常用于检查删除操作的结果。
RowDeleting
在单击某一行的“删除”按钮时,但在 GridView 控件从数据源中删除相应记录之前发生。此事件通常用于取消删除操作。
RowEditing
发生在单击某一行的“编辑”按钮以后,GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。
RowUpdated
发生在单击某一行的“更新”按钮,并且 GridView 控件对该行进行更新之后。此事件通常用于检查更新操作的结果。
RowUpdating
发生在单击某一行的“更新”按钮以后,GridView 控件对该行进行更新之前。此事件通常用于取消更新操作。
SelectedIndexChanged
发生在单击某一行的“选择”按钮,GridView 控件对相应的选择操作进行处理之后。此事件通常用于在该控件中选定某行之后执行某项任务。
SelectedIndexChanging
发生在单击某一行的“选择”按钮以后,GridView 控件对相应的选择操作进行处理之前。此事件通常用于取消选择操作。
Sorted
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之后发生。此事件通常用于在用户单击用于列排序的超链接之后执行某个任务。
Sorting
在单击用于列排序的超链接时,但在 GridView 控件对相应的排序操作进行处理之前发生。此事件通常用于取消排序操作或执行自定义的排序例程。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多