Solved

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

Posted on 2004-04-28
8
940 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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