Pagination and rowcommand in c#

452 Views Asked by At

In the code-behind of my cs page I have inserted the Pagination :

protected void Paginate(object sender, CommandEventArgs e)
{
    int intCurIndex = gvProducts.PageIndex;

    switch (e.CommandArgument.ToString().ToLower())
    {
        case "First":
            gvProducts.PageIndex = 0;
            break;
        case "Prev":
            gvProducts.PageIndex = intCurIndex - 1;
            break;
        case "Next":
            gvProducts.PageIndex = intCurIndex + 1;
            break;
        case "Last":
            gvProducts.PageIndex = gvProducts.PageCount - 1;
            break;
    }
    gvProducts.DataBind();
}

But this Pagination not working if adding the code-behind of my cs page the RowCommand event :

protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
    GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2");

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

This is the error, how to do resolve this :

Input string is not in correct format

On this line :

int rowindex = Convert.ToInt32(e.CommandArgument.ToString());

Edit #01

<PagerTemplate>
    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back_doppio.gif"
        CommandArgument="First" CommandName="Page" />
    <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back.gif"
        CommandArgument="Prev" CommandName="Page" />
    Page
        <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
            OnSelectedIndexChanged="DDLPages_SelectedIndexChanged">
        </asp:DropDownList>
    of
        <asp:Label ID="lblPageCount" runat="server"></asp:Label>
    <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif"
        CommandArgument="Next" CommandName="Page" />
    <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next_doppio.gif"
        CommandArgument="Last" CommandName="Page" />
</PagerTemplate>

Edit #02

  <asp:GridView ID="gvProducts" AutoGenerateColumns="False" EmptyDataText="Empty" EnableViewState="true"
        runat="server" DataKeyNames="sID" CssClass="mGrid" HorizontalAlign="Center"
        AllowPaging="True" PageSize="15"
        OnPageIndexChanging="gvProducts_PageIndexChanging"                 
        OnRowEditing="gvProducts_RowEditing"
        OnRowCancelingEdit="gvProducts_RowCancelingEdit"
        OnRowDataBound="gvProducts_RowDataBound"
        OnRowUpdating="gvProducts_RowUpdating"
        OnRowCommand="gvProducts_RowCommand">

Edit #03

Solved, thanks!

protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName != "Page")
    {
        int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
        GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2");

       if (e.CommandName == "Details")
       {
        ....
       }
       else
       {
       ....
       }
    }
}
0

There are 0 best solutions below