不工作我的gridview的分页功能

时间:2013-10-15 13:25:02

标签: c# gridview paging

enter image description here

我把我的问题放在这里,它显示在截图中。 我的代码如下。

.aspx文件的代码

 <asp:GridView ID="grid" runat="server" DataKeyNames="EventID" AutoGenerateColumns="false"
                            Width="100%" AllowPaging="True" OnRowDataBound="grid_RowDataBound" PageSize="3"
                            OnRowEditing="grid_RowEditing" OnRowUpdating="grid_RowUpdating" OnRowCancelingEdit="grid_RowCancelingEdit"
                            OnPageIndexChanging="grid_PageIndexChanging">
                            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NextPreviousFirstLast"
                                NextPageText="Next" PreviousPageText="Prev" PageButtonCount="3" />
                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Client Name
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblClientName" runat="server" Text='<%# Eval("ClientName")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtClientName" runat="server" Text='<%# Eval("ClientName")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Event Name
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblEventName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtEventName" runat="server" Text='<%# Eval("Name")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Invited Persons
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblNoOfInvitees" runat="server" Text='<%# Eval("No_Of_Invitees")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtNoOfInvitees" runat="server" Text='<%# Eval("No_Of_Invitees")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Total Invitees
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblTotalInvitees" runat="server" Text='<%# Eval("Total_Invitees")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtTotalInvitees" runat="server" Text='<%# Eval("Total_Invitees")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Extra Invitees
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblExtraInvitees" runat="server" Text='<%# Eval("Extra_Invitees")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtExtraInvitees" runat="server" Text='<%# Eval("Extra_Invitees")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Confirmed
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblConfirmed" runat="server" Text='<%# Eval("Extra_Invitees")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtConfirmed" runat="server" Text='<%# Eval("Extra_Invitees")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:CommandField ShowEditButton="true" HeaderText="Edit" />
                               <%-- <asp:TemplateField>
                                    <HeaderTemplate>
                                        Delete
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:LinkButton CommandName="Delete" ID="btnDelete" runat="server" Text="Delete"
                                            OnClientClick="return validate();" />
                                        <script type="text/javascript" language="javascript">
                                            function validate() {

                                                return confirm('Are you sure want to delete this user ?');
                                            }
                                        </script>
                                    </ItemTemplate>
                                </asp:TemplateField>--%>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td>
                        Showing
                        <asp:Label ID="PageIdxLabel" Text="1" runat="server"  />
                        to
                        <asp:Label ID="PageRowCountLabel" runat="server" />
                        of
                        <asp:Label ID="PageTotalLabel" runat="server" Text="Label" />
                    </td>
                </tr>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>

和aspx.cs文件编码在这里。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLayer;
using System.Data.Linq;
using System.Web.Extensions;
using System.Web.UI.WebControls.WebParts;


namespace EventManagerApp
{
    public partial class EventList : System.Web.UI.Page
    {
        EventManagerDataContext db = new EventManagerDataContext();


        [System.Web.Services.WebMethod]
        public static string[] GetNames(string prefixText)
        {
            EventManagerDataContext db = new EventManagerDataContext();

             return db.EMR_EVENTs.Where(n => n.Name.StartsWith(prefixText)).OrderBy(n => n.Name).Select(n => n.Name).ToArray();
             //return db.EMR_CLIENTs.Where(n => n.Name.StartsWith(prefixText)).OrderBy(n => n.Name).Select(n => n.Name).ToArray();
        }

        protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grid.EditIndex = e.NewEditIndex;

            grdBind();

        }

        protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            grid.EditIndex = -1;

            grdBind();

        }



        protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {




            int EventID = Convert.ToInt32(grid.DataKeys[e.RowIndex].Value.ToString());

            string ClientName = ((TextBox)grid.Rows[e.RowIndex].FindControl("txtClientName")).Text;

            string EventName = ((TextBox)grid.Rows[e.RowIndex].FindControl("txtEventName")).Text;

            int TotalInvitees = int.Parse(((TextBox)grid.Rows[e.RowIndex].FindControl("txtTotalInvitees")).Text);

            int ExtraInvitees = int.Parse(((TextBox)grid.Rows[e.RowIndex].FindControl("txtExtraInvitees")).Text);

            int Confirmed = int.Parse(((TextBox)grid.Rows[e.RowIndex].FindControl("txtConfirmed")).Text);

            EventManagerDataContext db = new EventManagerDataContext();
            var q = (from a in db.EMR_EVENTs
                     join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID
                     where a.EventID == EventID
                     select a).First();
            q.EMR_CLIENT.Name = ClientName;
            q.Name = EventName;
            q.No_Of_Invitees = TotalInvitees;
            q.Extra_Invitees = ExtraInvitees;
            q.Total_Invitees = ExtraInvitees + TotalInvitees;

            db.SubmitChanges();
            grid.EditIndex = -1;
            grdBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblwelcometext.Text = (string)(Session["name"]);
            try
            {
                if (!IsPostBack)
                {
                    grdBind();
                    bindClients();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        public class DALClass
        {
            private static EventManagerDataContext db = new EventManagerDataContext() { CommandTimeout = 36000 };


            public static List<EMR_CLIENT> GetClients()
            {
                return db.EMR_CLIENTs.ToList();
            }
        }

        protected void bindClients()
        {

            drpClient.DataSource = DALClass.GetClients();
            drpClient.DataTextField = "Name";
            drpClient.DataValueField = "ClientID";
            drpClient.DataBind();
            string Name = drpClient.SelectedItem.Value;
        }

        protected void grdBind()
        {
            try
            {

                EventManagerDataContext db = new EventManagerDataContext();

                if (txtSearchEvent.Text == "")
                {
                    var q = from a in db.EMR_EVENTs
                            join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID

                            select new
                            {
                                ClientName = b.Name,
                                ClientID = a.ClientID,
                                Name = a.Name,
                                No_Of_Invitees = a.No_Of_Invitees,
                                Extra_Invitees = a.Extra_Invitees,
                                Total_Invitees = a.Extra_Invitees + a.No_Of_Invitees,
                                EventID = a.EventID
                            };
                    grid.DataSource = q.ToList();
                    grid.PageSize = int.Parse(drpPageSize.SelectedValue);
                    grid.DataBind();

                }

                else
                {
                    var data = from a in db.EMR_EVENTs
                               join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID
                               where a.Name.StartsWith(txtSearchEvent.Text.Trim())
                               select new
                               {
                                   ClientName = b.Name,
                                   ClientID = a.ClientID,
                                   Name = a.Name,
                                   No_Of_Invitees = a.No_Of_Invitees,
                                   Extra_Invitees = a.Extra_Invitees,
                                   Total_Invitees = a.Extra_Invitees + a.No_Of_Invitees,
                                   EventID = a.EventID
                               };


                    grid.DataSource = data.ToList();
                    grid.PageSize = int.Parse(drpPageSize.SelectedValue);
                    grid.DataBind();
                }


            }

            catch
            {

                throw;
            }

        }

        protected void txtSearchEvent_TextChanged(object sender, EventArgs e)
        {
            grdBind();
        }


        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {

            grid.PageSize = int.Parse(drpPageSize.SelectedItem.Text);
            grdBind();

        }


        protected void drpClient_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (drpClient.SelectedIndex == 0)
            {
                grid.PageSize = int.Parse(drpPageSize.SelectedValue);
                grdBind();
            }
            else
            {
                var q = from a in db.EMR_EVENTs
                        join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID
                        where a.ClientID == int.Parse(drpClient.SelectedValue.ToString())
                        select new
                        {
                            ClientName = b.Name,
                            ClientID = a.ClientID,
                            Name = a.Name,
                            No_Of_Invitees = a.No_Of_Invitees,
                            Extra_Invitees = a.Extra_Invitees,
                            Total_Invitees = a.Extra_Invitees + a.No_Of_Invitees,
                            EventID = a.EventID
                        };
                grid.DataSource = q.ToList();
                grid.PageSize = int.Parse(drpPageSize.SelectedValue);
                grid.DataBind();
            }
        }





        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            grid.PageIndex = e.NewPageIndex;

           grdBind();

        }

        protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            EventManagerDataContext db = new EventManagerDataContext();

            var q = from a in db.EMR_EVENTs
                    join b in db.EMR_CLIENTs on a.ClientID equals b.ClientID


                    select new
                    {
                        ClientName = b.Name,
                        ClientID = a.ClientID,
                        Name = a.Name,
                        No_Of_Invitees = a.No_Of_Invitees,
                        Extra_Invitees = a.Extra_Invitees,
                        Total_Invitees = a.Extra_Invitees + a.No_Of_Invitees,
                        EventID = a.EventID
                    };

            PageTotalLabel.Text = q.Count().ToString();
            PageRowCountLabel.Text = grid.Rows.Count.ToString();

         //   PageIdxLabel.Text = grid.PageIndex.ToString();



        }

    }
}

所以我的问题是我想按照下一个和上一个按钮点击更新pageindex值。

1 个答案:

答案 0 :(得分:0)

这是我的问题的解决方案,我自己解决了。 谢谢stackoverflow。

   int pagetotal = q.Count(); 
   int pagerowcount = (grid.PageIndex * grid.PageSize) + grid.Rows.Count; 
   PageIdxLabel.Text = ((grid.PageIndex * grid.PageSize) + 1).ToString() + "to " +   Convert.ToString(pagerowcount) + "of" + (pagetotal).ToString();