Разбивка на страницы и команда строки в c # - PullRequest
0 голосов
/ 03 октября 2019

В коде моей cs-страницы я вставил Пагинацию :

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

Но эта Нумерация страниц не работаетесли при добавлении кода позади моей страницы cs событие RowCommand :

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

Это ошибка, как решить эту проблему:

Введенная строка имеет неправильный формат

В этой строке:

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

Изменить # 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>

Редактировать # 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">

Редактировать # 03

Решено, спасибо!

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