Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gridview paging is not working

I have a gridview control, however, when I click the page number, I get an error "Page not found". What am I missing here?

My code is:

<asp:GridView ID="gvEmployeeResults" Width="900px" CellSpacing="1" 
    CellPadding="2"  
    AutoGenerateColumns="false" OnRowDataBound="gvEmployeeResults_OnRowDataBound" 
    runat="server" AllowPaging="true" >
    <Columns>
        <asp:TemplateField HeaderText="Last Name, First Name" ItemStyle-Wrap="true" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left">
            <ItemTemplate>
                    <asp:LinkButton id="lbtnEmployeeName" OnCommand="EditEmployee_Command" CommandArgument='<%#Eval("EmployeeNum")%>' CommandName="EmployeeName" Visible="true" runat="server" ToolTip="Click to edit Employee."><%# DataBinder.Eval(Container.DataItem, "empLastName") + ", " + DataBinder.Eval(Container.DataItem, "empFirstName")%></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="empAddrLine1" ControlStyle-Width="225px" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left" HeaderText="Address" />
        <asp:BoundField DataField="empCity" ControlStyle-Width="120px" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left" HeaderText="City" />
        <asp:BoundField DataField="empState" ControlStyle-Width="50px" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left" HeaderText="State" />
        <asp:BoundField DataField="empPostalCode" ControlStyle-Width="100px" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left" HeaderText="Zip" />
        <asp:BoundField DataField="empDOB" ControlStyle-Width="100px" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left" HeaderText="Date Of Birth" />
    </Columns>
</asp:GridView>


        protected void BindGridview()
        {
            corpEmployee.Employee emp = new corpEmployee.Employee();

            emp.empLastName = tboxLastName.Text.Trim();
            emp.empFirstName = tboxFirstName.Text.Trim();
            emp.empDOB = tboxDateOfBirth.Text.Trim();

            gvEmployeeResults.DataSource = corpEmployeeMgr.GetEmployees(emp);
            gvEmployeeResults.DataBind();
        }

        protected void gvEmployeeResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text.Contains("nbsp;"))
                {
                    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&amp;", "&");
                }
            }
            else
            {
                return;
            }
        }

        protected void gvEmployeeResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEmployeeResults.PageIndex = e.NewPageIndex;
            gvEmployeeResults.DataBind();
        }
like image 279
DNR Avatar asked Jul 25 '12 13:07

DNR


2 Answers

you must use EnableViewstate= true and bind one time and use IsPostback. (PageIndex )

1. EnableViewState=true for your control

2. In the page load 

If(! IspostBack )
{
   Bind()....
}

And set PageIndex 
like image 133
Aghilas Yakoub Avatar answered Oct 12 '22 09:10

Aghilas Yakoub


You have to call BindGridview() in PageIndexChanging event handler.

protected void gvEmployeeResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gvEmployeeResults.PageIndex = e.NewPageIndex;
   BindGridview();
}
like image 39
KV Prajapati Avatar answered Oct 12 '22 07:10

KV Prajapati