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

  • SEARCH
  • Home
  • 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 8823609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:23:59+00:00 2026-06-14T06:23:59+00:00

First of all, I am a new C# developer and I need some help

  • 0

First of all, I am a new C# developer and I need some help please, I have a grid view with its SqlDataSource in my aspx file that contains 3 columns ID/Name/Job and several records(rows). When the user selects a row, I would like to redirect to another page and pass as a parameter the value of the selected ID of the row. Users are permitted to select only one row each time. I spent several hours on that however something strange is happened.

I have a method

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       string selectedID;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow gvr = e.Row;
            selectedID = (GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
            gvr.Attributes.Add("OnClick","javascript:location.href='Views/EditMenus/EditCompany.aspx?id=" + selectedID + "'");

            gvr.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFE6E6'");
            gvr.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            gvr.Attributes.Add("style", "cursor:pointer;");

            Session["IDs"] = selectedID;
     } }

In my redirect page I have in page load method the following code:

 protected void Page_Load(object sender, EventArgs e)
    {
          if (Session["IDs"] != null)
        {            
            Label2.Text = "Selected ID is: "+ Session["IDs"].ToString();
         }
    }

Now, when I select a row, the redirection to the other page works correctly and the browser url has the correct value of the selected ID according the javascript code above, however the Label2.Text prints a wrong ID. Instead of the value of the selected ID, it prints the value ID of the last row of each page. Why that happens?

It’s a bit strange for me since I am using the same variable “selectedID” for both cases as you can see above.

  • 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-14T06:24:01+00:00Added an answer on June 14, 2026 at 6:24 am

    You need to sore value of your ID in Session variable on SelectedIndexChanged event of your GridView

    void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
      {
    
        // Get the currently selected row.
        //Because the SelectedIndexChanging event occurs  before the select operation
        //in the GridView control, the SelectedRow property cannot be used.
        //Instead, use the Rows collection
        //and the NewSelectedIndex property of the 
        //e argument passed to this event handler.
    
        GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
    
        //Cells[0] is for first column so assign according to your column for ID
        Session["IDs"]=row.Cells[0].Text;
    
    
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first of all I'm totally new to FAST but I already have a couple
First of all I have to say I am new to iOS development. My
First of all I need to say that I'm new to jQuery. When I
First of all i'm new in here and new with csharp. just exhausted while
First of all, I'm quite new to the Android and JAVA world (coming from
First of all, I am absolutely new to WP. I am hoping to build
First of all, I'm really new to the MVC Asp.Net ideology. I would like
First of all, I'm fairly new to Java, so sorry if this question is
First of all, I am new to python/nltk so my apologies if the question
First of all, I am new to WPF and Xaml, so I just hope

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.