gridview中的PagerTemplate无法正常工作

时间:2014-12-19 15:42:28

标签: c# asp.net gridview pagertemplate

我不明白为什么会发生这种情况。

我有一个Gridview,我已经实现了 pagerTemplate 。它向我展示了来自数据库的正确记录。

首先,我采用了从here实现gridview寻呼机部分的逻辑。我的实现与他们所描述的相同。

现在我想出的情况是,当我更改下拉选择时,我的gridview会得到回发,并且网格的所有Row都会受到干扰。我不知道为什么会这样。

请参阅我实施的代码: -

<asp:GridView ID="grdCSRPageData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" CellPadding="3"
            AutoGenerateColumns="False" OnDataBound="grdCSRPageData_DataBound" AllowPaging="true" CssClass="hoverTable" EmptyDataText="No Records Found"
            OnPageIndexChanging="grdCSRPageData_PageIndexChanging" DataKeyNames="Id" OnRowDeleting="grdCSRPageData_RowDeleting"
            PageSize="4" ShowFooter="true" OnRowEditing="grdCSRPageData_RowEditing" OnRowUpdating="grdCSRPageData_RowUpdating"
            OnRowCancelingEdit="grdCSRPageData_RowCancelingEdit">
            <AlternatingRowStyle CssClass="k-alt" BackColor="#f5f5f5" />
            <Columns>
                <asp:TemplateField HeaderText="Select" HeaderStyle-Width="5%" HeaderStyle-CssClass="k-grid td">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="false" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="page_title" HeaderText="Page Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                <asp:BoundField DataField="page_description" HeaderText="Page Description" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                <asp:BoundField DataField="meta_title" HeaderText="Meta Title" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                <asp:BoundField DataField="meta_keywords" HeaderText="Meta Keywords" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                <asp:BoundField DataField="meta_description" HeaderText="Meta Description" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                <asp:BoundField DataField="Active" HeaderText="Active" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                <asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%" HeaderStyle-CssClass="k-grid td">
                    <ItemTemplate>
                        <asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png" UpdateImageUrl="~/images/update.png">
                    <ControlStyle Height="20px" Width="20px"></ControlStyle>
                </asp:CommandField>
            </Columns>
             <pagerstyle />
        <pagerTemplate>
            <table style="width:100%">
                <tr>
                    <td>
                        <asp:label id="MessageLabel" Text="Select a page:" runat="server"/>
            <asp:dropdownlist id="PageDropDownList" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" 
              runat="server"/>

                        <td style="width:70%; text-align:right">
                        <asp:label id="CurrentPageLabel"  runat="server"/>
          </td>

                    </td>
                </tr>
            </table>
        </pagerTemplate>
        </asp:GridView>

代码随PagerTemplate一起添加。另见我的代码背后给出的: - PagerTemplate的Cs代码: -

protected void grdCSRPageData_DataBound(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdCSRPageData.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
        if (pageList != null)
        {
            for (int i = 0; i < grdCSRPageData.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString());
                if (i == grdCSRPageData.PageIndex)
                {
                    item.Selected = true;
                }
                pageList.Items.Add(item);
            }
        }
        if (pageLabel != null)
        {
            int currentPage = grdCSRPageData.PageIndex + 1;
            pageLabel.Text = "Page " + currentPage.ToString() +
              " of " + grdCSRPageData.PageCount.ToString();
        }
}

 protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdCSRPageData.BottomPagerRow;
        DropDownList pagelist = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        grdCSRPageData.PageIndex = pagelist.SelectedIndex;
    }

请帮忙。任何帮助都会很明显

1 个答案:

答案 0 :(得分:1)

尝试将dropDownList的AutoPostBack="true"更改为false,并确保在Page_Load中加载数据

if (!IsPostBack){
  //bind gridview
}

<强>更新

 尝试将您的dropDownList(带有AutoPostBack="true")和GridView插入UpdatePanel,并"UpdateMode=Conditional"像这样

  <asp:UpdatePanel runat="server" ID="up1" UpdateMode="Conditional">
    <ContentTemplate>
      //put here gridview
    <ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="PageDropDownList" EventName="SelectedIndexChanged" />
    </Triggers>
  </asp:UpdatePanel>

<强>更新

使用您的代码尝试此解决方案

aspx页面

    <asp:GridView ID="grdUser"
     AllowPaging="true"
     AutoGenerateColumns="False"
     OnDataBound="grdUser_DataBound"
     OnRowDeleting="grdUser_RowDeleting"
     OnPreRender="PreRenderGrid"
     runat="server"
     Width="100%"
     border="1"
     DataKeyNames="Id"
     PageSize="2"
     OnPageIndexChanging="grdUser_PageIndexChanging"
     EnableSortingAndPagingCallbacks="false"
     CssClass="pagi" OnRowCommand="grdUser_RowCommand">
 <Columns>
 <asp:TemplateField HeaderText="Select" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<ItemTemplate>
 <asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>

  <HeaderStyle CssClass="k-grid td"></HeaderStyle>

  <ItemStyle Width="30px"></ItemStyle>
  </asp:TemplateField>

  <asp:BoundField DataField="username" HeaderText="UserName" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
  <HeaderStyle CssClass="k-grid td"></HeaderStyle>

  <ItemStyle Width="30px"></ItemStyle>
 </asp:BoundField>
 <asp:BoundField DataField="email" HeaderText="Email Id" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
  <HeaderStyle CssClass="k-grid td"></HeaderStyle>

       <ItemStyle Width="30px"></ItemStyle>
 </asp:BoundField>
 <asp:BoundField DataField="usertype" HeaderText="UserType" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
 <HeaderStyle CssClass="k-grid td"></HeaderStyle>

 <ItemStyle Width="30px"></ItemStyle>
 </asp:BoundField>
 <asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
   <HeaderStyle CssClass="k-grid td"></HeaderStyle>

     <ItemStyle Width="30px"></ItemStyle>
 </asp:BoundField>
   <asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%" ItemStyle-Width="20" HeaderStyle-CssClass="k-grid td">
     <ItemTemplate>
   <asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="eEdit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
  <asp:ImageButton ID="btnEdit" AlternateText="Edit" ImageUrl="~/images/edit.png" runat="server" Width="15" Height="15" CommandName="eEdit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" />
 </ItemTemplate>

 <HeaderStyle CssClass="k-grid td" Width="15%"></HeaderStyle>

 <ItemStyle Width="30px"></ItemStyle>

 </asp:TemplateField>
 </Columns>

  <PagerStyle ForeColor="#e3e3e3"
                        BackColor="#e3e3e3" CssClass="grid-pagi" />
  <PagerTemplate>
   <table runat="server" id="testTable1" style="width: 100%" class="k-grid td">
                            <tr>
                                <td class="col-md-7 pull-left">
                                    <asp:Label ID="MessageLabel"
                                        Text="Select a page:"
                                        runat="server" />
                                    <asp:LinkButton ID="FirstLB" runat="server" CommandName="Page" CommandArgument="First" ToolTip="First" CssClass="btn-pager btn-default"><<</asp:LinkButton>
                                    <asp:LinkButton ID="PrevLB" runat="server" CommandName="Page" CommandArgument="Prev" ToolTip="Previous" CssClass="btn-pager btn-default"><</asp:LinkButton>
                                    <asp:DropDownList runat="server" ID="PageDropDownList" AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" CssClass="selectpicker form-control-drp"></asp:DropDownList>

                                    <asp:LinkButton ID="NextLB" runat="server" CommandName="Page" CommandArgument="Next" ToolTip="Next" CssClass="btn-pager btn-default">></asp:LinkButton>
                                    <asp:LinkButton ID="LastLB" runat="server" CommandName="Page" CommandArgument="Last" ToolTip="Last" CssClass="btn-pager btn-default">>></asp:LinkButton>
                                </td>

                                <td class="col-md-3 pull-right">
                                    <asp:Label ID="PageSizeLabel" runat="server" Text="Select Page Size: "></asp:Label>
                                    <asp:DropDownList ID="ddlPageSize" runat="server" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" AutoPostBack="true" CssClass="selectpicker form-control-drp">
                                        <%-- <asp:ListItem Value="0" Text="0" />--%>
                                        <asp:ListItem Value="1" Text="1" />
                                        <asp:ListItem Value="2" Text="2" />
                                        <asp:ListItem Value="3" Text="3" />
                                    </asp:DropDownList>
                                </td>
                                <td class="col-md-2">
                                    <asp:Label ID="CurrentPageLabel" runat="server" />
                                </td>
                            </tr>
                        </table>
                    </PagerTemplate>
                </asp:GridView>

背后的代码

protected void grdUser_DataBound(object sender, EventArgs e)
        {
            GridViewRow pagerRow = grdUser.BottomPagerRow;
            DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
            if (Context.Session["PageSize"] != null)
            {
                pageSizeList.SelectedValue = Context.Session["PageSize"].ToString();
            }
            DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
            Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

            if (pageList != null)
            {
                for (int i = 0; i < grdUser.PageCount; i++)
                {
                    int pageNumber = i + 1;
                    ListItem item = new ListItem(pageNumber.ToString());
                    if (i == grdUser.PageIndex)
                    {
                        item.Selected = true;
                    }
                    pageList.Items.Add(item);
                }
            }

            if (pageLabel != null)
            {
                int currentPage = grdUser.PageIndex + 1;
                pageLabel.Text = "View " + currentPage.ToString() + " of " + grdUser.PageCount.ToString();
            }
        }
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow pagerRow = grdUser.BottomPagerRow;
            DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
            //

            grdUser.PageSize = Convert.ToInt32(pageSizeList.SelectedValue);
            Context.Session["PageSize"] = pageSizeList.SelectedValue;
            BindGrid();
        }
        protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow pagerRow = grdUser.BottomPagerRow;
            DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
            grdUser.PageIndex = pageList.SelectedIndex;
            BindGrid();
        }

 protected void PreRenderGrid(object sender, EventArgs e)
        {
            GridViewRow pagerRow = grdUser.BottomPagerRow;
            DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");//error
            Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
            if (pageList != null)
            {
                for (int i = 0; i < grdUser.PageCount; i++)
                {
                    int pageNumber = i + 1;
                    ListItem item = new ListItem(pageNumber.ToString());
                    if (i == grdUser.PageIndex)
                    {
                        item.Selected = true;
                    }
                    pageList.Items.Add(item);
                }
            }
            if (pageLabel != null)
            {
                int currentPage = grdUser.PageIndex + 1;
                pageLabel.Text = "View " + currentPage.ToString() + " of " + grdUser.PageCount.ToString();
            }
            this.grdUser.Controls[0].Controls[this.grdUser.Controls[0].Controls.Count - 1].Visible = true;
            BindGrid();
        }