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 6713867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:48+00:00 2026-05-26T08:25:48+00:00

Background : I have a GridView which gets populated from an SqlDataSource via DataSourceID.

  • 0

Background:

I have a GridView which gets populated from an SqlDataSource via DataSourceID. The rows show some summary data from an SQL View. Upon clicking a row, I would like to take my user to another page with a DetailsView control which gets populated with the full set of values from the DB related to the row clicked. My user should be able to edit the data, download files associated with the record, and create a new record of a different type based on said data.

Error:

All examples that I’ve found for Clickable GridView rows end up with some variation of the error Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.

Naturally, I do not want to expose my site to vulnerabilities by disabling event validation. I need to be able to grab the Primary key of the clicked row’s associated record and perform operations on that data on a subsequent page, probably via a DetailsView. I suspect my errors are a result of my setup, which is why I included those details.

My Questions Are:

  1. How do I capture the Primary Key of clicked row?
  2. How do I, onclick, forward to a “details” page with a pre-filled form containing data from the row record that was clicked?

HERE’S THE COMPLETE SOLUTION

**thanks again to Icarus’ help

'Fetch the DataKey ("ID"), seems to work
Protected Sub RowBind(ByVal sender As Object, ByVal e As GridViewRowEventArgs) _
    Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim datakey As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()
    End If
End Sub
'Handle button click
Protected Sub RowClick(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) _
    Handles GridView1.RowCommand

    If e.CommandName = "Select" Then
        'Add to session variable; translate the index of clicked to Primary Key
        Session.Add("DetailsKey", GridView1.DataKeys(e.CommandArgument).Value.ToString)
        Response.Redirect("details.aspx")
    End If
End Sub

And My Markup

<asp:GridView ID="GridView1" runat="server" DataSourceID="GridView1SDS"
    DataKeyNames="ID" AllowPaging="True" AllowSorting="True">
    '<!-- Styling -->
    <Columns>
        <asp:ButtonField ButtonType="Button" Text="Details" CommandName="Select" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="GridView1SDS" runat="server"
    ConnectionString="<%$ ConnectionStrings:dbConnectionString %>"
    SelectCommand="select * from viewRequestQueue">'<!-- An SQL View -->
</asp:SqlDataSource>

Forwarded Page VB & Markup

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        GridView2SDS.SelectCommand = "select * from viewRequestQueue where ID = " _
            + Session.Item("DetailsKey").ToString
    End If
End Sub

<asp:DetailsView ID="GridView2" runat="server" DataSourceID="GridView2SDS">
    '<!-- Styling -->
</asp:DetailsView>
<asp:SqlDataSource ID="GridView2SDS" runat="server"
    ConnectionString="<%$ ConnectionStrings:dbConnectionString %>">
</asp:SqlDataSource>

Also, please note that if the SelectCommand for your DataSource is handled in the codebehind, that means the DataBind will overwrite your <Columns> in the markup. To get around this, you should define the columns in the code behind before the DataBind. So say I wanted to add another ButtonField column to my forwarded page (notice the SelectCommand is not provided in the markup), I added the following before the setting the SelectCommand and doing the DataBind:

Dim id As New ButtonField()
id.ButtonType = ButtonType.Button
id.Text = "Load"
id.CommandName = "Select"
PubDetails.Columns.Add(id)
  • 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-05-26T08:25:49+00:00Added an answer on May 26, 2026 at 8:25 am

    You need to declare the KeyNames of the items you are binding on your markup. For example:

    <asp:GridView id="grid" runat="Server" DataSourceID="YourSQLDataSource" DataKeyNames="ID,Name" />
    

    In your case, you seemed to be handling the OnRowDataBound event. You can do this to grab the key inside RowBound:

    If e.Row.RowType = DataControlRowType.DataRow Then
      Dim datakey As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()  'get the datakey
    End If
    

    Your second question is difficult to answer because you did not specify how you want the user to be redirected, if from the client side using Javascript or from the Server side. You also did not specify how do you expect to populate the details on the Details page. Do you expect to read a parameter from the URL and use it to get the record details from the database?

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

Sidebar

Related Questions

Background: I have a ListView / GridView with several columns. In some situations, only
I am working on an ASP.Net website and have a GridView displaying some data,in
Hi have a repeater which is inside a gridview. when i bind the data
BACKGROUND: I have a GridView based on a fairly complex Adapter class which extends
I have a gridview that has some 20 columns and 1000 rows. The grid
I have a gridview with some data bound to it. I also have different
Background: we have an application that generates reports from HTML (that may or may
Background: I have a module which declares a number of instance methods module UsefulThings
I have created a table in ASP MVC and populate it with data from
Background I have a GridView where input in Column1 depends on the input Column2

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.