Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:13:50+00:00 2026-06-09T17:13:50+00:00

Why is it that when I try to set my GridView’s sorting using session

  • 0

Why is it that when I try to set my GridView’s sorting using session states that suddenly my GridView no longer has DataKeys? All I did was put the following code into my Page_Init;

Dim SortDirection As String = Session("SortDir")
Dim sortExpression As String = Session("SortExp")
If Not SortDirection Is Nothing AndAlso Not sortExpression Is Nothing Then
    If (SortDirection = "asc") Then
        GridView1.Sort(sortExpression, WebControls.SortDirection.Ascending)
    Else
        GridView1.Sort(sortExpression, WebControls.SortDirection.Descending)
    End If
End If

But if I comment this out than my other methods don’t crash out any more as my GridView now has it’s DataKeys. Why is this?

UPDATE

This is the exact line that stops working when the above code is in place.

Dim UserID = GridView1.DataKeys(e.RowIndex).Value.ToString

According to debugger GridView1 has columns but it’s DataKeys Count is 0. The error I receive is;

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:13:52+00:00Added an answer on June 9, 2026 at 5:13 pm

    You want to perform those actions in the Page_Load (possibly in a If Not Page.IsPostBack block) event, not in the Page_Init event. Init is for initializing or reading control properties; Load is where you generally set properties (like sort direction, etc).

    Basically, your ViewState hasn’t been loaded in Page_Init yet. So, you modify the controls properties in Init, then some properties get filled out from the ViewState, and this leads to unexpected behavior when your Page does the Load event (which recursively calls each server control’s Load event).

    You can read all about this (somewhat confusing) topic on MSDN: ASP.NET Page Life Cycle Overview

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my problem is that when i try to set the text of service1 and
Im currently facing the problem that when i try to set focus on some
Greetings, what is the problem that when I try to set credentials for my
I have a table that displays a set of data. But when I try
Any ideas why on some links that I try to access using HttpWebRequest I
Has anyone managed to extend a GridView with default sorting? This seems pretty trivial
I am currently using Watir with Firefox and it seems that when I try
According to my book all that is needed to start using automatic paging is
I am doing insert/update and delete in gridview. For that I am using ItemTemplate
I have a user control that has a gridview in it with paging. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.