locked
Pagination and rowcommand in c# RRS feed

  • Question

  • User1151703306 posted

    Hello there, I hope in your help.

    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();
        }
    
        <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>

    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());

    Can you help me ?

    Thursday, October 3, 2019 1:19 PM

Answers

  • User1151703306 posted

    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
           {
           ....
           }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 5:43 AM

All replies

  • User1151703306 posted

    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
           {
           ....
           }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 5:43 AM
  • User61956409 posted

    Hi Golia,

    Glad to hear you have solved the problem.

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

    If the CommandArgument is "First",  "Prev", and "Last" etc, to convert it to integer directly with above code snippet, which would cause the issue.

    With Regards,

    Fei Han

    Friday, October 4, 2019 8:55 AM