Click here to Skip to main content
16,018,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When using gridview paging this error occur please help me:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.]
System.Web.UI.WebControls.GridView.OnPageIndexChanging(GridViewPageEventArgs e) +1587673
System.Web.UI.WebControls.GridView.HandlePage(Int32 newPage) +62
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +434
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +205
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9643314
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

This is My GridView code:
XML
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="4" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="Ridge" BorderWidth="2px"
        CellPadding="3" DataKeyNames="ReplyDoubtID" OnRowCommand="GridView1_RowCommand"
        CssClass="roundedbox" CellSpacing="1" GridLines="None">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Sender Name" SortExpression="Name" />
            <asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
            <asp:TemplateField HeaderText="Topic">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Topic") %>' TextMode="MultiLine"
                        Height="54px" Width="202px" CssClass="roundedbox"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Solved Doubts">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ReplyDoubt") %>' TextMode="MultiLine"
                        Height="84px" Width="239px" CssClass="roundedbox"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Download">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Files") %>'
                        Text='<%# Eval("Files") %>' CommandName="Download" OnClick="LinkButton1_Click"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Rate">
                <ItemTemplate>
                    <cc1:Rating ID="Rating1" runat="server" StarCssClass="Star" WaitingStarCssClass="WaitingStar"
                        EmptyStarCssClass="Star" FilledStarCssClass="FilledStar" Height="22px" Width="107px"
                        AutoPostBack="True" OnChanged="OnRatingChanged" CurrentRating='<%# Eval("Rating") %>'>
                    </cc1:Rating>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>


This is my code behind:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Data;
using AjaxControlToolkit;

namespace Virtual_Classroom.Student
{
    public partial class Inbox : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = GetData("SELECT ReplyDoubtID,Name,Subject,ReplyDoubt,Date,Files,FileName,Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt");
                GridView1.DataBind();
            }
        }

        private static DataTable GetData(string query)
        {
            DataTable dt = new DataTable();
            string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        sda.Fill(dt);
                    }
                }
                return dt;
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
                Response.TransmitFile(Server.MapPath("~/Files/") + e.CommandArgument);
                Response.End();
            }
        }

        protected void OnRatingChanged(object sender, RatingEventArgs e)
        {
            int rowIndex = ((sender as Rating).NamingContainer as GridViewRow).RowIndex;
            int ReplyDoubtID = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
            string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Rates VALUES(@ReplyDoubtID, @Rating)"))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@ReplyDoubtID", ReplyDoubtID);
                    cmd.Parameters.AddWithValue("@Rating", e.Value);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            Response.Redirect(Request.Url.AbsoluteUri);
        }




    }
}
Posted
Comments
King Fisher 30-Mar-15 13:13pm    
Fire up you gridview PageIndexChanging Event.;)

  • You have AllowPaging="True" set on the GridView;
  • You're binding the grid from the code-behind, rather than from a data-source control;
  • You haven't handled the PageIndexChanging event;

When you don't use a data-source control, the grid doesn't know where to get its data, so you have to handle various events to re-bind the grid.

The simplest option would be to add a handler for the event:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Moved to a separate method, as we don't want to repeat the DataSource:
        BindGrid();
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGrid();
}

private void BindGrid()
{
    GridView1.DataSource = GetData("SELECT ReplyDoubtID, Name, Subject, ReplyDoubt, Date, Files, FileName, Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt");
    GridView1.DataBind();
} 


Another option would be to use model binding[^] to bind the grid:
aspx
<asp:GridView ID="GridView1" runat="server"
    ...
    SelectMethod="GridView1_GetData"
>

C#
// NB: No need for Page_Load or GridView1_PageIndexChanging handlers.

protected DataView GridView1_GetData()
{
    DataTable dt = GetData("SELECT ReplyDoubtID, Name, Subject, ReplyDoubt, Date, Files, FileName, Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt");
    return dt.DefaultView;
}


Or the SqlDataSource control[^]:
aspx
<asp:GridView ID="GridView1" runat="server"
    ...
    DataSourceID="GridView1DataSource"
>
...
</asp:GridView>

<asp:SqlDataSource id="GridView1DataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:VCRConnectionString %>"
    SelectCommand="SELECT ReplyDoubtID, Name, Subject, ReplyDoubt, Date, Files, FileName, Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt"
/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900