Solved

ASP.NET DataGrid - Clickable Record that stores session variable and redirects to new page

Posted on 2004-04-28
8
936 Views
Last Modified: 2007-12-19
I was reading the following solution (about making a row a clickable record that stores a session variable and redirects to a new page) answered by dante469 and tried it myself - and have run into problems:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20912914.html#10555087

Can someone (maybe dante469) look at this and see if you know what i'm doing wrong:

Hi I put in the code and have a clickable record.  However, when I click on it, it just changes colors like the standard "Select" feature in the datagrid.  I'm using both paging and column sort (without view state - its built in my code) - Could this be affecting it?
==================================================
Here's the databinding code:
Sub BindData()
    Dim RIDsent As integer
    Dim StatusSent as integer
    Dim OurConnectionString As String
    Dim OurConnection as SqlConnection
    Dim OurCommand as SqlCommand
    Dim OurDataAdapter as SqlDataAdapter
    Dim OurDataSet as new DataSet
    Dim Role as integer
    'Retrieve Region ID from Session Variable and assign status as active-1
    RIDsent = session("RID")
    StatusSent = "1"
'   Authenticate against role 2
    Role = Cint(session("Role"))
    If Role >= "2"
    'Set the grid's page from viewstate
        OurDataGrid.CurrentPageIndex = CInt(Viewstate("pageIndex"))
        OurConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
        OurConnection = new SqlConnection(OurConnectionString)
        OurCommand = new SqlCommand("spFilterRegion" , OurConnection)
        OurCommand.CommandType = CommandType.StoredProcedure
        OurCommand.Parameters.Add("@RIDsent", SqlDbType.Int, 4).Value = RIDsent
        OurCommand.Parameters.Add("@StatusSent", SqlDbType.Int, 4).Value = StatusSent
        OurCommand.Parameters.Add("@SelectedRegion",sqlDBType.NVarChar, 50).Direction = ParameterDirection.Output
        OurDataAdapter = new SqlDataAdapter(OurCommand)
        OurDataAdapter.Fill(OurDataSet, "Employees")
        Dim OurDataTable as new DataView(OurDataSet.Tables("Employees"))
    'Sets the sort field
        if OurSortField > " " Then
            OurDataTable.Sort = OurSortField
        end if
    'Bind data to datagrid
        OurDataGrid.DataSource = OurDataTable
        OurDataGrid.Databind()
    'Load the Region Title for the logged user
        Dim RegionTitle As String = OurCommand.Parameters("@SelectedRegion").Value
        RegionLabel.Text = RegionTitle.ToString
    'Load Employee Name
        LoginName.Text = session("Name") & " logged in"
    End If
End Sub
====================================
Here's the code for paging:

html:
OnPageIndexChanged="PageIndexChanged_OnClick"runat="server" AllowPaging="True"

VB Script:
'For Setting the Page Index
Sub PageIndexChanged_OnClick(Sender As Object, E As DataGridPageChangedEventArgs)
    Viewstate("pageIndex") = e.NewPageIndex
    BindData()
End Sub
=====================================
Here's the code for sorting

html on datagrid:<...OnSortCommand="DataSort" runat="server" And AllowSorting="True"

VB-Script:
'For Sorting the Datagrid
Sub DataSort (sender As Object, e As DataGridSortCommandEventArgs)
    OurSortField = e.SortExpression
    OurLabel.Text = "You sorted by " + OurSortField
    BindData()
End Sub
======================================
Here's the column select code:

html:
<asp:ButtonColumn DataTextField="FullName" SortExpression="FullName" HeaderText="FullName" CommandName="Select"></asp:ButtonColumn>

VB Script:
'When an Employee is Selected
Private Sub OurDataGrid_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
     Select Case e.CommandName
        Case Is = "select"
            Session("SEID") = e.Item.Cells(0).Text  
            response.redirect("ProducerView.aspx")
        end select
End Sub
=========================================
Can you see what I'm doing wrong?  Or will this simply not work if I am using paging or column sorting?

0
Comment
Question by:djacus
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10946256
Have you set the ItemCommand event handler?

OurDataGrid.ItemCommand += new DataGridCommandEventHandler(OurDataGrid_ItemCommand);
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 125 total points
ID: 10946576
when you use 'string  = string' expressions it is case sensitive
so when you click on the button in your datagrid you submit CommandName="Select" but you check if it is "select" so the problem is that these strings are not equal

B..M
0
 

Author Comment

by:djacus
ID: 10951195
Brilliant,

Actually needed to do both - add the event handler and correct the case - Thank You!

It is now bringing me to the correct page, but not assigning the session variable correctly.
I think that's because I really don't know if I'm assigning it correctly in the script:
Session("SEID") = e.Item.Cells(0).Text

I am storing the EmployeeID field in the datagrid (as invisible, but first column) and have assigned it as the DataKeyField.  And it's a integer.

Since I don't understand what's in cell(0), I'm not sure I've done this correctly - any ideas??

DJ
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:djacus
ID: 10951376
dfiala and mmarinov,

my mistake - it is storing the correct session variable -My test is wrong on the receiving page.  Thanks for your help!!!

dj
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10951396
Since it's your key field, you don't need to go to the cell itself.  This will work...

Session("SEID") = OurDataGrid.DataKeys(e.Item.ItemIndex)
0
 

Author Comment

by:djacus
ID: 10952054
dfiala13,

I like that better; it makes more sense to me to know it's the key field.

Just curious - is there a website that explains how the cells are assigned - what's in cell 0?

thanks

dj
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10952291
The cells either have controls or text in them, depending on what the column type is for the cell and whether or not the item is in edit mode. When a cell is in view mode, you typically just will have a value in the cell's Text property.

Here's a link to the Cells property

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolstablerowclasscellstopic.asp

You'll notice that it actually represents a collection of TableCells (which was what a DataGrid ultimately renders the browser).

And here are all the properties for an individual cell...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolstablecellmemberstopic.asp
0
 

Author Comment

by:djacus
ID: 10952694
Many thanks!!  I hope someday I am an expert!  You're very helpful

dj
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now