Solved

Allow paging of a GridView does not move to the record on the next page.

Posted on 2009-07-08
3
400 Views
Last Modified: 2013-12-17
This is my Gridview:
      <asp:GridView ID="GrdVwBuyUpContributions" runat="server" AutoGenerateColumns="False"
                                                           
            OnRowEditing="GrdVwBuyUpContributions_Click" OnRowUpdating="GrdVwBuyUpContributions_update" OnRowCancelingEdit="GrdVwBuyUpContributions_RowCancelingEdit" OnPageIndexChanging="GrdVwBuyUpContributions_PageIndexChanging"
            BackColor="White" BorderColor="#E7E7FF"
            BorderStyle="None" BorderWidth="1px" GridLines="Horizontal" HorizontalAlign="Left" DataKeyNames="Contribution_ID"
            Width="300" AllowPaging="True" PageSize="10" >
            <Columns>
                <asp:CommandField ShowEditButton="True"></asp:CommandField>
                <asp:TemplateField HeaderText="Contribution Date" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <%# Eval("Date") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="DateTextBox" Text='<%# Eval("Date")%>' runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="You must enter a valid date."
                            ControlToValidate="DateTextBox">
                            *</asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="You must provide a valid Date.."
                            ControlToValidate="DateTextBox" Operator="DataTypeCheck" Type="Date">
                            *</asp:CompareValidator>
                        <cc1:CalendarExtender ID="CalendarExtender1" TargetControlID="DateTextBox" Format="yyyy/MM/dd"
                            runat="server">
                        </cc1:CalendarExtender>
                     
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Amount" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                       <%# string.Format("{0:c}", Eval("Amount"))%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="AmountTextBox" Text='<%# string.Format("{0:0.00}", Eval("Amount"))%>' runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2"  runat="server" ErrorMessage="You must enter an amount."
                            ControlToValidate="AmountTextBox">
                            *</asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="You must provide a valid currency value for the Amount."
                            ControlToValidate="AmountTextBox" Operator="DataTypeCheck" Type="Currency">
                            *</asp:CompareValidator>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
        </asp:GridView>

This is the code when pageIndex changes:

 protected void GrdVwBuyUpContributions_PageIndexChanging(Object sender, GridViewPageEventArgs e)
     {

       // Cancel the paging operation if the user attempts to navigate
       // to another page while the GridView control is in edit mode.
       if (GrdVwBuyUpContributions.EditIndex != -1)
       {
         // Use the Cancel property to cancel the paging operation.
         e.Cancel = true;

         // Display an error message.
         int newPageNumber = e.NewPageIndex + 1;
         lblMessage.Text = "Please update the record before moving to page " +
           newPageNumber.ToString() + ".";
       }
       else
       {
         // Clear the error message.
         lblMessage.Text = "";
         
       }

However when I click on any of the page numbers on the page, the gridview does not do what it is supposed to do.

Why?
0
Comment
Question by:Faye_DBA
3 Comments
 
LVL 15

Accepted Solution

by:
jinal earned 500 total points
ID: 25041014

 protected void GrdVwBuyUpContributions_PageIndexChanging(Object sender, GridViewPageEventArgs e)

     {
 

       // Cancel the paging operation if the user attempts to navigate

       // to another page while the GridView control is in edit mode.

       if (GrdVwBuyUpContributions.EditIndex != -1)

       {

         // Use the Cancel property to cancel the paging operation.

         e.Cancel = true;
 

         // Display an error message.

         int newPageNumber = e.NewPageIndex;

         lblMessage.Text = "Please update the record before moving to page " +

           newPageNumber.ToString() + ".";

       }

       else

       {

         // Clear the error message.

         GrdVwBuyUpContributions.PageIndex = e.NewPageIndex;

         lblMessage.Text = "";

         

       }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data is not showing from images 15 36
Error on Add method 1 36
how to read json value 2 27
Run time Error 4 34
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now