Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Due to the way our CMS works all bespoke pages have to be in a compiled class library (VB.NET). I need to be able to update a datatable in a Session based on the selected value of a dropdown list in a GridView.

Either my SelectedIndexChanged event handler is not firing, or there is a problem with referring to the odtCourseBasket from the handler. I had to change the handler from Shared to Public as otherwise the code errored with "Cannot refer to an instance member of a class"

Relevant code for the gridview in Basket.vb :
VB
Public Sub Basket_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

     dsDisplay = getCourses()
     oGridview.Columns.Clear()
     oGridview.AutoGenerateColumns = False

     Dim tf As New TemplateField
     tf.ItemTemplate = New GridViewDatesTemplate("courseID", "String", DataControlRowType.DataRow)
     oGridview.Columns.Add(tf)
     oGridview.DataSource = dsDisplay.Tables(0)
     oGridview.DataBind()


Code for GridViewDatesTemplate:
Public Class GridViewDatesTemplate Implements System.Web.UI.ITemplate
'code snipped for brevity    

    Public Property odtCourseBasket() As DataTable
'code snipped for brevity
    End Property


    Sub New(ByRef colname As String, ByRef DataType As String, ByRef type As 
'code snipped for brevity
    End Sub

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
                Dim ddlDates As New DropDownList
                ddlDates.ID = "ddDates"
                ddlDates.ClientIDMode = ClientIDMode.Static
                ddlDates.AutoPostBack = True
                AddHandler ddlDates.DataBinding, New EventHandler(AddressOf Item_DataBinding)
                AddHandler ddlDates.SelectedIndexChanged, New EventHandler(AddressOf Item_SelectedIndexChanged)
                container.Controls.Add(ddlDates)
    End Sub

    Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

'Code snipped: Fetches courseid, startdate from MIS system that match the course code & academic year of the original courseid passed in. SelectedValue of dropdown list set to original courseid passed in - done because I can't put a postback check around the ddl creation
    End Sub

    Public Sub Item_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'Attempts to replace courseid in basket session with that attached to selectedvalue in the ddl
        Dim ddlDates As New DropDownList  = CType(sender, DropDownList)
        Dim row As GridViewRow = CType(ddlDates.NamingContainer, GridViewRow)

        Dim newcourseid As String = ddlDates.SelectedValue.ToString()
        Dim oldcourseid As String = DataBinder.Eval(row.DataItem, "courseID").ToString()
        Dim dtTable As New DataTable = odtCourseBasket

        Dim old As Int32 = -1

        For z As Integer = 0 To (dtTable.Rows.Count - 1)
            If dtTable.Rows(z)("CourseID").ToString = oldcourseid Then
                old = z
            End If
        Next
        If old > -1 Then
            dtTable.Rows.RemoveAt(old)
        End If
        dtTable.Rows.Add(newcourseid)
        odtCourseBasket = dtTable
    End Sub
End Class
Posted
Updated 29-Jan-14 1:35am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900