0

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
       {
       ....
       }
    }
}
  • When the `Paginate` method is called? What value you are getting in `e.CommandArgument`? When `gvProducts_RowCommand` is called? – Chetan Oct 03 '19 at 12:10
  • Are yo u setting row index to e.CommandArgument in aspx? – Chetan Oct 03 '19 at 12:12
  • You can look at https://stackoverflow.com/questions/6503339/get-row-index-on-asp-net-rowcommand-event to know how it get row index in RowCommand event. – Chetan Oct 03 '19 at 12:13
  • @ChetanRanpariya Thanks for help, please see **Edit #01** in my first question –  Oct 03 '19 at 13:01
  • You have commandargument values set as "First", "Last", "Next", "Prev".... this values can not be converted to integer... that's why you are getting this error.. – Chetan Oct 03 '19 at 13:57
  • @ChetanRanpariya I have changed "First", "Last", "Next", "Prev" with "1", "2", "3", "4" and not error but the pagination not working –  Oct 03 '19 at 14:02
  • Which event of grid view is registered with `Paginate` method? Looks like when you click on page number it executes RowCommand event and not the Pageinate method... – Chetan Oct 03 '19 at 14:04
  • @ChetanRanpariya Thanks for help, please see **Edit #02** in my first question –  Oct 03 '19 at 14:10
  • In RowCommand event you should first check if the e.CommandName != "Page" . You should skip rest of the code in RowCommand event if e.CommandName == "Page" check this https://stackoverflow.com/questions/6952256/gridview-rowcommand-event-is-fired-when-i-do-sorting-in-gridview – Chetan Oct 03 '19 at 14:10
  • 1
    @ChetanRanpariya Thank you! Solution on **Edit #03** in my first question –  Oct 03 '19 at 14:16

0 Answers0