在更新面板内为gridview进行分页

时间:2014-08-27 06:07:38

标签: asp.net updatepanel

网格视图分页在Updatepanel

中无法正常工作

我尝试了很多属性但没有工作

<ContentTemplate>
    <asp:GridView ID="gvRef" runat="server" CellPadding="3" EnableSortingAndPagingCallbacks="true"
        CssClass="mGrid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" AllowSorting="true"
        AutoGenerateColumns="False" DataKeyNames="id" OnPageIndexChanging="gvRef_PageIndexChanging"
        PageSize="5" AllowPaging="true" OnRowDataBound="gvRef_RowDataBound" OnSorting="gvRef_Sorting" OnRowCommand="gvRef_RowCommand">
        <PagerSettings Mode="Numeric" />
        <RowStyle />
        <Columns>
            <asp:TemplateField HeaderText="Opening Code" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblOpId" runat="server" Text='<%# Eval("id")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="OP#">
                <ItemTemplate>
                    <asp:Label ID="lblOpeningCode" runat="server" Text='<%# Eval("opening_code")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Client">
                <ItemTemplate>
                    <asp:Label ID="lblClientName" runat="server" Text='<%# Eval("Client")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Opening Name" SortExpression="opening_name">
                <ItemTemplate>
                    <asp:Label ID="lblOpName" ToolTip='<%# Eval("COUNTRY")%>' Width="120px" runat="server" Text='<%# Eval("opening_name")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Submitted By" SortExpression="submitted_by_name">
                <ItemTemplate>
                    <asp:Label ID="lblAddedBy" runat="server" Text='<%# Eval("submitted_by_name")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Submitted Date" SortExpression="submitted_date">
                <ItemTemplate>
                    <asp:Label ID="lblSubmittedDate" runat="server" Text='<%# Eval("submitted_date")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Candidate Name">
                <ItemTemplate>
                    <asp:HyperLink ID="hlAssign" ToolTip='<%# "Marital Status : " + Eval("marital_status") +"\n"+"Payroll type    : " + Eval("EMP_TYPE")+"\n"+"Profile Status : " + Eval("profile_status")%>' runat="server" NavigateUrl='<%# "~/Admin/ViewConsultant.aspx?id=" +  Eval("id") %>'>

                        <asp:Label ID="lblClient" Width="120px" runat="server" Text='<%# Eval("candidate_name")%>'></asp:Label>
                    </asp:HyperLink>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Notice Period">
                <ItemTemplate>
                    <asp:Label ID="lblNotice" runat="server" Text='<%# Eval("notice_period")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Exp. Years">
                <ItemTemplate>
                    <asp:Label ID="lblExp" runat="server" Text='<%# Eval("exp_years")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Current Package">
                <ItemTemplate>
                    <asp:Label ID="lblCurrentPkg" runat="server" Text='<%# Eval("current_package")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Offered Package">
                <ItemTemplate>
                    <asp:Label ID="lblOffPkg" runat="server" Text='<%# Eval("proposed_package")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Current Location" Visible="false">
                <ItemTemplate>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Current Org. / Location">
                <ItemTemplate>
                    <asp:Label ID="lblCurrORg" runat="server" Text='<%# Eval("current_organization")%>'></asp:Label>
                    <br />
                    <asp:Label ID="lblCurrLoc" runat="server" Text='<%# Eval("current_location")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Contact Details">
                <ItemTemplate>
                    <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("email_address")%>'></asp:Label><br />
                    <asp:Label ID="lblContactNumber" runat="server" Text='<%# Eval("contact_number")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Key Skills">
                <ItemTemplate>

                    <asp:Label ID="lblKeySkills" ToolTip='<%#Eval("key_skills")%>' runat="server" Width="80" Text='<%#  Eval("key_skills").ToString().Length>15 ? Eval("key_skills").ToString().Substring(0,14) : (Eval("Key_skills") != null ? Eval("key_skills"):"")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
                <ItemStyle Width="8%" HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>

                    <asp:HyperLink ID="hlEdit" Width="50" runat="server" NavigateUrl='<%# "~/Admin/AddEditConsultant.aspx?id=" +  Eval("id") %>'>E Profile</asp:HyperLink>

                    <br />
                    <asp:HyperLink ID="HyperLink2" Width="50" runat="server" NavigateUrl='<%# "~/Admin/UpdateConsultentstatus.aspx?id=" +  Eval("id") %>'>E Status</asp:HyperLink>
                    <%--   <asp:LinkButton ID="lbStatus" runat="server" CausesValidation="false" OnClick="lbStatus_Click" Text="E Status"></asp:LinkButton>--%>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Profile">
                <ItemTemplate>
                    <asp:HyperLink ID="hldownload" ToolTip="Base Profile" runat="server" NavigateUrl='<%# "~/Admin/handlers/filehandler.ashx?id=" +  Eval("id") %>' Text="BP"></asp:HyperLink>
                    <asp:HyperLink ID="hlFp" runat="server" ToolTip="Formatted Profile" NavigateUrl='<%# "~/Admin/handlers/filehandler.ashx?id=" +  Eval("id") +"&type=formatted" %>' Text="FP"></asp:HyperLink>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" Width="5%" />
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Change Status" Visible="false">
                <ItemTemplate>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" Width="1%" />
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>

                    <asp:CheckBox ID="chkAll" runat="server" onclick="javascript: SelectAllCheckboxes1(this);" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSubmit" runat="server" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" Width="1%" />
            </asp:TemplateField>

        </Columns>
        <PagerStyle Font-Bold="True" Font-Size="14pt" />


        <EmptyDataTemplate>
            <b>No Profiles ...</b>
        </EmptyDataTemplate>
        <HeaderStyle />
        <AlternatingRowStyle CssClass="alt" />
    </asp:GridView>
</ContentTemplate>  
</asp:UpdatePanel>

代码背后

protected void gvRef_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
      gvRef.PageIndex = e.NewPageIndex;
      LoadGridView();
      upd.Update();
}

1 个答案:

答案 0 :(得分:0)

首先,paging is not working不是澄清问题所在的最佳陈述。所以我假设你点击一个新的pageIndex - 没有(??)发生。

所以我尝试通过编写一个小型演示来解决您的问题。我创建了一个包含所有templateFields值的演示数据类。将这些值绑定到GridView和PageIndexChanged - 我将新索引绑定并重新绑定值。

<强> demoGridView.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
    <asp:UpdatePanel  ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="gvRef" runat="server" CellPadding="3" AllowSorting="true" AutoGenerateColumns="False" DataKeyNames="id"
                PageSize="5" AllowPaging="true" EnableSortingAndPagingCallbacks="true"
                OnPageIndexChanging="gvRef_PageIndexChanging"
                OnRowDataBound="gvRef_RowDataBound"
                OnSorting="gvRef_Sorting"
                OnRowCommand="gvRef_RowCommand">
                YOUR GRIDVIEW CODE GOES HERE
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

<强> demoGridView.aspx.cs

public partial class demoGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // bind data only on first load of page
        if (!Page.IsPostBack)
        {
            #region init demo Data            
            gvRef.DataSource = LoadData();
            gvRef.DataBind();
            #endregion
        }
    }

    // helper method to simulate data loaded from any kind of storage
    private object LoadData()
    {
        List<Data> data = new List<Data>();
        for (int i = 0; i < 100; i++)
        {
            data.Add(new Data()
            {
                id = i,
                opening_code = "openingCode" + i,
                Client = "Client" + i,
                COUNTRY = "COUNTRY" + i,
                opening_name = "opening_name" + i,
                submitted_by_name = "submitted_by_name" + i,
                submitted_date = DateTime.Now,
                marital_status = "marital_status" + i,
                EMP_TYPE = "EMP_TYPE" + i,
                profile_status = "profile_status" + i,
                candidate_name = "candidate_name" + i,
                notice_period = "notice_period" + i,
                exp_years = i,
                current_package = "current_package" + i,
                proposed_package = "proposed_package" + i,
                current_organization = "current_organization" + i,
                current_location = "current_location" + i,
                email_address = "email_address" + i,
                contact_number = "contact_number" + i,
                key_skills = "key_skills" + i
            });
        }
        return data;
    }

    // set new page index, reload/-bind data
    protected void gvRef_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        /// skipped e.newPageIndex validation - todo

        gvRef.PageIndex = e.NewPageIndex;
        gvRef.DataSource = LoadData();
        gvRef.DataBind();
    }
    protected void gvRef_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        /// todo
    }
    protected void gvRef_Sorting(object sender, GridViewSortEventArgs e)
    {
        /// todo
    }
    protected void gvRef_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /// todo
    }
}

// demo class - testing purposes only
public class Data
{
    public int id { get; set; }
    public string opening_code { get; set; }
    public string Client { get; set; }
    public string COUNTRY { get; set; }
    public string opening_name { get; set; }
    public string submitted_by_name { get; set; }
    public DateTime submitted_date { get; set; }
    public string marital_status { get; set; }
    public string EMP_TYPE { get; set; }
    public string profile_status { get; set; }
    public string candidate_name { get; set; }
    public string notice_period { get; set; }
    public int exp_years { get; set; }
    public string current_package { get; set; }
    public string proposed_package { get; set; }
    public string current_organization { get; set; }
    public string current_location { get; set; }
    public string email_address { get; set; }
    public string contact_number { get; set; }
    public string key_skills { get; set; }
}  

我遇到的唯一问题是我的web.config未正确配置。我错过了一些处理程序/模块aso。如果有一些脚本无法加载,您可能会在浏览器中检查您的开发控制台

之后 - 它就像一个魅力。

HTH