简体   繁体   中英

ASP.NET GridView row selection not working in page 2 and above

I am using a gridview that contains 4 columns as below and i am using the OnSelectedIndexChanged and OnPageIndexChanging attributes with page size as 5.

<asp:GridView ID="grid_data" PageSize="5" OnPageIndexChanging="grid_data_PageIndexChanging" OnSelectedIndexChanged="grid_data_SelectedIndexChanged" ShowFooter="true" AllowSorting="True" AllowPaging="true" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Data ID" SortExpression="ID" />
        <asp:BoundField DataField="Name" HeaderText="Person Name" SortExpression="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
        <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
        <asp:CommandField ShowSelectButton="true" SelectText="View" CausesValidation="false" />
    </Columns>
</asp:GridView>

Whenever i click on the View from command field a new window should open where i have to display the name and age of the person only and for that i have done the code below.

protected void grid_data_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["NAME"] = grid_data.Rows[grid_data.SelectedRow.DataItemIndex].Cells[1].Text.ToString();
    Session["AGE"] = grid_data.Rows[grid_data.SelectedRow.DataItemIndex].Cells[2].Text.ToString();
    ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( 'Details.aspx', null, 'height=768,width=1024,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=0,left=0,resizable=yes' );", true);
}

I am fetching the name and age from columns 1 and 2 and storing them in session and then opening a new window and there i will display these values stored in session.

Initially I have a button i my page and when i click on that button the query will be executed and fetches data and populates in grid view as below.

protected void button_getdata_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["String"].ConnectionString);
    con.Open();
    SqlCommand get = new SqlCommand("select * from PersonTable", con);
    SqlDataAdapter data = new SqlDataAdapter(get);
    DataTable dataTable = new DataTable();
    data.Fill(dataTable);
    ViewState["DATA"] = dataTable;
    grid_data.DataSource = dataTable;
    grid_data.DataBind();
    con.Close();
}

Then i have also done page handling as below by storing the data table in view state.

protected void grid_data_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grid_data.PageIndex = e.NewPageIndex;
    grid_data.DataSource = ViewState["DATA"];
    grid_data.DataBind();
}

Now after fetching data when i click on View from the first page of gridview it works, i can see the new window with the required values. But when i go to next page the page is loading perfectly due to page handling but in any other page apart from the first page if i click on View i get the following error.

Error: Sys.WebForms.PageRequestManagerServerErrorException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

When i tried to check online i came to know this is because we are using page size and paging together we have to use some other method to identify the row selected instead of using grid_data.Rows[grid_data.SelectedRow.DataItemIndex].Cells[1].Text.ToString(); . But i am not sure of what to do and how to do that. Is there any other way to access the rows in all pages correctly? Or am i doing something else wrong?

Also whenever i try to sort the columns i am getting the following error.

Error: Sys.WebForms.PageRequestManagerServerErrorException: The GridView 'grid_data' fired event Sorting which wasn't handled.

How can i handle the sorting thing?

EDIT

I found this link for sorting and this really works.

GridView.Sorting Event (System.Web.UI.WebControls)

Concerning the sorting problem:

Your code behind contains an event handler for paging, but not one for sorting. So add an event handler to the code behind. Then specify that as the OnSorting handler.

Ex:

<asp:GridView ID="grid_data" PageSize="5" AllowSorting="True" OnSorting="grid_data_Sorting" OnPageIndexChanging="grid_data_PageIndexChanging" OnSelectedIndexChanged="grid_data_SelectedIndexChanged" ShowFooter="true" AllowPaging="true" runat="server" AutoGenerateColumns="false">

and

protected void grid_data_Sorting(object sender, GridViewSortingEventArgs e)
{        
    //sort dataset
    grid_data.DataBind();
}

Concerning the selected item problem:

I would suggest passing the personid to the details.aspx page through querystring instead of session. Just change a column in the grid to contain a link to details.aspx?personid={whatever}. And add target='_blank' to get it to open in a new window. The details page should read the personid from the querystring, pull from database, and show the data for that person.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM