GridviewPager不路由onpaging事件

时间:2015-06-14 16:55:10

标签: asp.net pager

我的asp.net gridviewpager有问题。当我点击寻呼机的任何页面时,它都会路由到OnRowCommand事件。我等着路由OnPageIndexChanging 我已经像这样定义了我的gridview。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
        GridLines="None" PageSize="4" AllowPaging="True" EmptyDataText="No record found"
        OnPageIndexChanging="OnPaging" OnRowDeleting="GridView1_RowDeleting" OnRowCommand="RowCommand"
        CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" />
            <asp:BoundField DataField="Baslik" HeaderText="Baslik" />
            <asp:BoundField DataField="KisaAciklama" HeaderText="Kısa Acıklama" />
            <asp:TemplateField HeaderText="Güncelle">
                <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("Id") %>' CommandName="VIEW">Güncelle</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Sil">
                <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lnkDelete" CommandArgument='<%#Eval("Id") %>'
                        CommandName="DELETE">Sil</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <%----%>
        </Columns>
    </asp:GridView>

我的后端代码也是

  protected void RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LinkButton lnkView = (LinkButton)e.CommandSource;
        string Id = lnkView.CommandArgument;
        if (e.CommandName == "VIEW")
        {
            Response.Redirect("/Views/AdminPages/Kayit.aspx?cmd=Update&id="+Id);

        }
        else if (e.CommandName == "DELETE")
        {

            kayitService.DeleteKayit(Convert.ToInt32(Id));
        }
    }


    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

1 个答案:

答案 0 :(得分:0)

请试试这个

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" BackColor="White"  

BORDERCOLOR =&#34;#3366CC&#34;边框=&#34;无&#34;边框宽度=&#34; 1px的&#34; CELLPADDING =&#34; 4&#34;
    onpageindexchanging =&#34; gvPerson_PageIndexChanging&#34;
    onrowcancelingedit =&#34; gvPerson_RowCancelingEdit&#34;
    onrowdatabound =&#34; gvPerson_RowDataBound&#34; onrowdeleting =&#34; gvPerson_RowDeleting&#34;
    onrowediting =&#34; gvPerson_RowEditing&#34; onrowupdating =&#34; gvPerson_RowUpdating&#34;
    onsorting =&#34; gvPerson_Sorting&#34;&GT;                                         

    <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Baslik" HeaderText="Baslik" />
        <asp:BoundField DataField="KisaAciklama" HeaderText="Kısa Acıklama" />
        <asp:TemplateField HeaderText="Güncelle">
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("Id") %>' CommandName="VIEW">Güncelle</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Sil">
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="lnkDelete" CommandArgument='<%#Eval("Id") %>'
                    CommandName="DELETE">Sil</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <%----%>

</Columns> 

后端代码可能是:

protected void gvPerson_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // Set the index of the new display page.  
        Gridview1.PageIndex = e.NewPageIndex;
        // Rebind the GridView control to  
        // show data in the new page. 
        BindGridView();
    }

根据我的观点,我上面提到的代码是最合适的分页代码。