We help IT Professionals succeed at work.

passing session variables in gridview.

NP2322 asked
Last Modified: 2008-04-24
I currently have a gridview that has 4 columns. I've used RowDataBound to make the third column clickable so that a new page will open up and display some more results. What it is suppose to do is take the value from the 4th column, put it in a session variable and then send it to the new page. What is ACTUALLY happening is the program take the value of the LAST row in the gridview and then passes it along. So if i were to say select the first row, it would still take the value of the last row and then send it out.

Below is my code:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Session("FAC_ID") = e.Row.Cells(3).Text
            e.Row.Cells(2).Attributes.Add("onclick", "javascript:window.open('Calibration/Calibration_view.aspx')")
        End If
    End Sub

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
    DataSourceID="sqlFacilityHealthManager" ForeColor="#333333" GridLines="None" Width="262px" OnRowDataBound="GridView1_RowDataBound">
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" />
    <EditRowStyle BackColor="#2461BF" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
        <asp:BoundField DataField="FACILITY_SHORT" HeaderText="Facility" SortExpression="FACILITY_SHORT" />
        <asp:BoundField DataField="BUILDING" HeaderText="Building" SortExpression="BUILDING" />
        <asp:ImageField DataImageUrlField="IMAGE_LOCATION" HeaderText="Status" />
        <asp:BoundField DataField="FAC_ID" HeaderText="FAC_ID" InsertVisible="False" ReadOnly="True"
            SortExpression="FAC_ID" ShowHeader="False" />
<asp:SqlDataSource ID="sqlFacilityHealthManager" runat="server" ConnectionString="<%$ ConnectionStrings:FacilityDataConnectionString %>"
    SelectCommand="SELECT Facility_Sub_Data.FACILITY_SHORT, Facility_Status_Data.IMAGE_LOCATION, Facility_Sub_Data.BUILDING, Facility_Sub_Data.FAC_ID FROM Facility_Manager_Data INNER JOIN Facility_Sub_Data ON Facility_Manager_Data.FM_ID = Facility_Sub_Data.FACILITY_MANAGER INNER JOIN Facility_Status_Data ON Facility_Sub_Data.FACILITY_STATUS = Facility_Status_Data.S_ID WHERE (Facility_Manager_Data.FACILITY_MANAGER = @FACILITY_MANAGER)">
        <asp:SessionParameter DefaultValue="" Name="FACILITY_MANAGER" SessionField="fullname" />

Any kind of help would be teriffic, Thanks!
Watch Question

It's because you're setting the Session variable in the RowDataBound event so it's always going to be set with the value of the last row that's databound. Rather than using a BoundField for the last column try using a TemplateField.
...that way you can control what happens when it's clicked. Let me know if you need me to clarify anything.


So even if i change the bound field to a template field, my session variable is still in the RowDataBound event. so then it would still return the last row.

also, i've started using the templatefield anyway just to try it out but i'm not sure how to bind the values to this new template. this is what i have so far:

                <asp:Label runat ="server" ID='Label1' />

I'm not sure what else to add to the label to bind it to 'FAC_ID'.

Either way, i still have the session variable in the rowdatabound event.

Ok, what you want to do is this:

1. Setup a SelectedIndexChanged event handler for the GridView
2. Keep your boundfield for the FAC_ID field
3. Create a TemplateField that'll hold a button of some sort with CommandName="Select" (this will allow you to select a row)
4. When the row is selected (inside the SelectedIndexChanged handler) grab the FAC_ID associated with that row and set the Session variable. Then do a Response.Redirect to the page you want to go to.

... hope that helps.


Ok, so it's kind of working. The problem is now, when i click on it, it's still using the variable from the previous click. So if first click the button and the value is 27. I click the second button and the value is still 27, not 54.

I was gonna use session.clear() but that wouldn't work cause then i would get any value.

any other ideas?
are you grabbing the value from the selected row?


   Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(4).Attributes.Add("onclick", "javascript:window.open('Calibration/Calibration_view.aspx')")
        End If
    End Sub
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FAC_ID") = Me.GridView1.SelectedRow.Cells(3).Text
    End Sub

That's what i have right now.

any thoughts?
I think it may be because you're still using the javascript to redirect.


But what if i want to open a new window and not just redirect the current page?
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.