3

我不明白为什么会这样。

我有一个 Gridview,在里面我实现了一个pagerTemplate。它向我显示来自数据库的正确记录。

首先,我从这里获取了实现 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;
    }

请帮忙。任何帮助都将是可观的

4

1 回答 1

1

尝试更改 dropDownList 的AutoPostBack="true"tofalse并确保在 Page_Load 里面加载数据

if (!IsPostBack){
  //bind gridview
}

更新

尝试将您的 dropDownList (with 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>

更新

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

页面

    <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();
        }
于 2014-12-19T15:58:50.733 回答