• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 943
  • Last Modified:

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

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
djacus
Asked:
djacus
  • 4
  • 3
1 Solution
 
dfiala13Commented:
Have you set the ItemCommand event handler?

OurDataGrid.ItemCommand += new DataGridCommandEventHandler(OurDataGrid_ItemCommand);
0
 
mmarinovCommented:
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
 
djacusAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
djacusAuthor Commented:
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
 
dfiala13Commented:
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
 
djacusAuthor Commented:
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
 
dfiala13Commented:
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
 
djacusAuthor Commented:
Many thanks!!  I hope someday I am an expert!  You're very helpful

dj
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now