Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web user control event handling troubles

Status
Not open for further replies.

chadau

Programmer
Nov 14, 2002
155
US
I have a web user control that contains a Datalist control. I am binding data to the datalist. Everything works fine until I need to trap a button click event that is supposed to handle the Delete command for the Datalist control. I listed the code below in hopes that someone can point out something I am missing here. Thanks.

The first block of code below is in my ASP.NET Web form.
Code:
Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Me.ESOItemList = LoadControl("ESOItemList.ascx")
        Me.ESOItemList.DataSource = Me.AddESOItem
        Me.ESOItemList.DataBind()
        Me.ESOItemList.EditMode = True

        Me.PlaceHolder1.Controls.Add(Me.ESOItemList)
    End Sub

    Private Function AddESOItem() As ItemDetailCollection
        Dim objItem As New ItemDetail
        Dim objList As New ItemDetailCollection

        If Not Me.Session.Item("ItemList") Is Nothing Then
            objList = Me.Session.Item("ItemList")
        End If

        With objItem
            .UPNumber = "55555"
            .SerialNumber = ""
            .Quantity = "5"
            .PriceCode = 2
            .PriceCodeDesc = "Price Agreed"
            ' = "2"
            .Price = "$60.00"
            .Description = "Testing"
        End With

        objList.Add(objItem)

        If Not Me.Session.Item("ItemList") Is Nothing Then
            Me.Session.Item("ItemList") = objList
        Else
            Me.Session.Add("ItemList", objList)
        End If

        Return objList
    End Function

The code block below if from my Web user control.
Code:
Public Class ESOItemList
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dlstDetailItems As System.Web.UI.WebControls.DataList
    Protected WithEvents btn1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Public Event RemoveItem(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

    Private _objItems As ItemDetailCollection
    Private _bEditMode As Boolean

    Public Property DataSource() As ItemDetailCollection
        Get
            Return Me._objItems
        End Get
        Set(ByVal Value As ItemDetailCollection)
            Me._objItems = Value
        End Set
    End Property

    Public Property EditMode() As Boolean
        Get
            Return Me._bEditMode
        End Get
        Set(ByVal Value As Boolean)
            Dim ibtn As New ImageButton
            Dim i As Int16

            For i = 0 To Me.dlstDetailItems.Items.Count - 1
                ibtn = Me.dlstDetailItems.Items(i).FindControl("ibtnRemove")
                If Value = False Then
                    ibtn.Visible = False
                Else
                    ibtn.Visible = True
                End If
            Next i

            Me._bEditMode = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
    End Sub

    Private Sub Page_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DataBinding
        Me.dlstDetailItems.DataSource = Me.GetESOItemList
        Me.dlstDetailItems.DataBind()
    End Sub

    Private Function GetESOItemList() As DataTable
        Dim objItem As ItemDetail
        Dim dt As New DataTable
        Dim drow As DataRow
        Dim i As Int16

        dt = Me.CreateDataTable()

        If Not Me._objItems Is Nothing Then
            If Me._objItems.Count <> 0 Then
                For i = 0 To Me._objItems.Count - 1
                    objItem = New ItemDetail
                    objItem = Me._objItems(i)

                    drow = dt.NewRow
                    With drow
                        .Item("UPNumber") = UCase(objItem.UPNumber)
                        .Item("SerialNumber") = UCase(objItem.SerialNumber)
                        .Item("Quantity") = objItem.Quantity
                        .Item("PriceCode") = objItem.PriceCodeDesc
                        .Item("PriceCodeID") = objItem.PriceCode
                        .Item("Price") = objItem.Price
                        .Item("Description") = objItem.Description
                    End With
                    dt.Rows.Add(drow)
                Next
                Return dt
            End If
        End If
    End Function

    Private Function CreateDataTable() As DataTable
        Dim dt As New DataTable

        With dt.Columns
            .Add("UPNumber")
            .Add("SerialNumber")
            .Add("Quantity")
            .Add("PriceCode")
            .Add("PriceCodeID")
            .Add("Price")
            .Add("Description")
        End With

        Return dt
    End Function

    Private Sub dlstDetailItems_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstDetailItems.DeleteCommand
        RaiseEvent RemoveItem(Me, e)
    End Sub

    '''Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    '''    RaiseEvent ButtonClick(Me, e)
    '''End Sub

    '''Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

    '''End Sub

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        Me.dlstDetailItems.DataSource = Me.GetESOItemList
        Me.dlstDetailItems.DataBind()
    End Sub
End Class
 
1) Thats a lot of code to expect someone to read for you !
2) what happens if you press the delete button ? (Have you watched in the debugger to see if anything happens ?)

When the control redraws, are you catching the isPostBack, or are you deleting it, then redrawing the existing data ? (Bear with me if this isnt the exact event name, its been a while since Ive played with c#)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top