Solved

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

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

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

697 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