Solved

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

Posted on 2004-04-28
8
934 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

15 Experts available now in Live!

Get 1:1 Help Now