This is a migrated thread and some comments may be shown as answers.

ItemCommand not firing in a custom server control

5 Answers 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alfredo Correa
Top achievements
Rank 1
Alfredo Correa asked on 22 Jul 2009, 02:52 PM

Hi!
I have created a custom server control  which implements your data grid.
Everything works fine except that when I click the “delete” button it does not fire the “ItemCommand” event. It does go to the server however.
I have attached my code here so you can take a look.
Thank you in advance!


Imports
System

Imports System.Web.UI.WebControls

Imports System.Web.UI

Imports Telerik.WebControls

Imports System.ComponentModel

<DefaultProperty(""), ToolboxData("<{0}:npaGeneralDataGrid runat=server></{0}:npaGeneralDataGrid>")> _

Public Class npaGeneralDataGrid

    Inherits Global.Telerik.WebControls.RadGrid

    Private _ShowColumnEmployeeNumber As Boolean = False

    Private _ShowColumnDeleteNPA As Boolean = False

    Private _iconDeleteNPA As String

    Private _GridType As GridTypeEnum

    'Private oUserProfile As NPABusinessServices.UserProfile

    Private _dataGridOwner As String

#Region "Custom Properties"

    Public Property GridType() As GridTypeEnum

        Get

            Return _GridType

        End Get

        Set(ByVal value As GridTypeEnum)

            _GridType = value

        End Set

    End Property

    Public Property ShowColumnEmployeeNumber() As Boolean

        Get

            Return _ShowColumnEmployeeNumber

        End Get

        Set(ByVal value As Boolean)

            _ShowColumnEmployeeNumber = value

        End Set

    End Property

    Public Property ShowColumnDeleteNPA() As Boolean

        Get

            Return _ShowColumnDeleteNPA

        End Get

        Set(ByVal value As Boolean)

            _ShowColumnDeleteNPA = value

        End Set

    End Property

    Public Property iconDeleteNPA() As String

        Get

            Return _iconDeleteNPA

        End Get

        Set(ByVal value As String)

            _iconDeleteNPA = value

        End Set

    End Property

#End Region

#Region "Grid Type Enumeratior"

    Public Enum GridTypeEnum As Integer

        Drafts = 0

    End Enum

#End Region

    Public Sub New()

        MyBase.New()

        MyBase.AutoGenerateColumns = False

    End Sub

    Private Sub npaGeneralDataGrid_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        Dim oCol As GridColumn

        oCol = New EmployeeNumber

        oCol.HeaderText = "Emp No."

        oCol.SortExpression = "LastName"

        oCol.HeaderStyle.Width = Unit.Pixel(60)

        oCol.HeaderStyle.HorizontalAlign = WebControls.HorizontalAlign.Left

        oCol.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Left

        oCol.Visible = Me.ShowColumnEmployeeNumber

        MyBase.MasterTableView.Columns.Add(oCol)

        oCol = New DeleteNPA(_iconDeleteNPA)

        MyBase.MasterTableView.Columns.Add(oCol)

        oCol.HeaderText = ""

        oCol.HeaderStyle.HorizontalAlign = WebControls.HorizontalAlign.Left

        oCol.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Left

        oCol.Visible = Me.ShowColumnDeleteNPA

    End Sub

    Private Sub npaGeneralDataGrid_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ''data gets binded here - works beautifully

    End Sub

    Private Sub npaGeneralDataGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles Me.ItemCommand

        ''this is never fired

        Select Case e.CommandName.ToUpper()

            Case "DELETE"

            Case "DESELECT"

            Case "PAGE"

        End Select

    End Sub

End Class

''class containing the column definition

Imports System

Imports System.Data

Imports System.Configuration

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports Telerik.WebControls

Public Class EmployeeNumber

    Inherits GridTemplateColumn

    Private Class ActualClass

        Implements ITemplate

        Public Column As EmployeeNumber = Nothing

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

            Dim oLabel As New Label

            oLabel.ID = "lblEmployeeNumber"

            container.Controls.Add(oLabel)

            AddHandler oLabel.DataBinding, AddressOf BindObject

        End Sub

        Public Sub BindObject(ByVal sender As Object, ByVal e As EventArgs)

            'Create a new instance of a Label.

            Dim oLabel As Label = CType(sender, Label)

            Dim container As GridDataItem = CType(oLabel.NamingContainer, GridDataItem)

            'Bind the employee name to the label text

            If Not IsDBNull(container.DataItem("EmployeeNumber")) Then

                oLabel.Text = container.DataItem("EmployeeNumber")

            End If

        End Sub

    End Class

    Public Sub New()

        Dim template As New ActualClass

        template.Column = Me

        Me.ItemTemplate = template

    End Sub

End Class

Public Class LastUpdatedDate

    Inherits GridTemplateColumn

    Private Class ActualClass

        Implements ITemplate

        Public Column As LastUpdatedDate = Nothing

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

            Dim oLabel As New Label

            oLabel.ID = "lblLastUpdatedDate"

            container.Controls.Add(oLabel)

            AddHandler oLabel.DataBinding, AddressOf BindObject

        End Sub

        Public Sub BindObject(ByVal sender As Object, ByVal e As EventArgs)

            'Create a new instance of a Label.

            Dim oLabel As Label = CType(sender, Label)

            Dim container As GridDataItem = CType(oLabel.NamingContainer, GridDataItem)

            'Bind the employee name to the label text

            oLabel.Text = container.DataItem("LastUpdatedDate")

        End Sub

    End Class

    Public Sub New()

        Dim template As New ActualClass

        template.Column = Me

        Me.ItemTemplate = template

    End Sub

End Class

Public Class DeleteNPA

    Inherits GridTemplateColumn

    Private Shared _IconPath As String

    Private Class ActualClass

        Implements ITemplate

        Public Column As DeleteNPA = Nothing

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

            Dim oImageButton As New ImageButton

            oImageButton.ID = "imgDeleteNPA"

            oImageButton.ImageUrl = _IconPath

            'oImageButton.CommandName = "delete"

            container.Controls.Add(oImageButton)

            AddHandler oImageButton.DataBinding, AddressOf BindObject

        End Sub

        Public Sub BindObject(ByVal sender As Object, ByVal e As EventArgs)

            ''Get the label's instance

            Dim oImageButton As ImageButton = CType(sender, ImageButton)

            Dim container As GridDataItem = CType(oImageButton.NamingContainer, GridDataItem)

            ''Bind the object's CommandArgument to the NPAID

            oImageButton.CommandArgument = container.DataItem("NPAID")

            oImageButton.CommandName = "delete"

        End Sub

    End Class

    Public Sub New(ByVal iconPath As String)

        Dim template As New ActualClass

        template.Column = Me

        Me.ItemTemplate = template

        Me.AllowFiltering = False

        _IconPath = iconPath

    End Sub

End Class

5 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 23 Jul 2009, 08:11 AM

Hello Alfredo,

I reviewed your code snippets and noticed that you add the DeleteNPA template column inside the Init handler first (before defining its properties):

oCol = New DeleteNPA(_iconDeleteNPA)  
 
        MyBase.MasterTableView.Columns.Add(oCol)  
 
        oCol.HeaderText = "" 
 
        oCol.HeaderStyle.HorizontalAlign = WebControls.HorizontalAlign.Left  
 
        oCol.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Left  
 
        oCol.Visible = Me.ShowColumnDeleteNPA  
 

This is in conflict with the concepts of dynamic RadGrid creation (entirely programmatically from the code-behind) listed here:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

I suggest you move the line which adds the column to the Columns collection as last in the code snippet as this will ensure that the viewstate of the column and the lifecycle of the controls in it will remain intact.

You may also define the CommandName="Delete" value for the image button (which is part of the template column) directly inside the InstantiateIn overridden method.

Kind regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Alfredo Correa
Top achievements
Rank 1
answered on 23 Jul 2009, 11:35 AM

Sebastian,

Thank you for your reply.

I initially had the “add” column lines at the end of the code snippets for “deleteNPA”. I was just moving it around to see if the order affected the outcome… I was actually following a suggestion from the “Programmatic creation” article:

boundColumn = New GridBoundColumn()
       tableViewOrders.Columns.Add(boundColumn)
       boundColumn.DataField = "OrderDate"
       boundColumn.HeaderText = "Date Ordered"

I had also defined the CommandName inside the InstantiateIn method. I just also moved it around to see if it produced another result.

Anyways, I have moved them back to their original position (the one suggested by you) but still not working.

I have previously built custom controls using your RadComboBox object and they all have worked flawlessly. It would be fantastic if I can get this issue resolved promptly. Is there any other thing that might be causing my code not to work properly?

Once again thank you for your help.

Alfredo

0
Sebastian
Telerik team
answered on 23 Jul 2009, 12:09 PM
Hello Alfredo,

The method you depicted would be correct if you set the grid statically on the page and build its columns programmatically inside the OnLoad handler inside !Page.IsPostBack condition (see the help topic for reference).

I assume that in your case you create the entire control programmatically and generate its columns inside the OnInit handler. In this case you need to define the properties of the columns first and then add them to the Columns collection because the Init event is fired before LoadViewState in the page lifecycle.

Furthermore, can you verify that the viewstate of the grid (or this of the page/application which hosts it) is enabled in this case? This is required in order image buttons to trigger the event bubbling mechanism of the grid and subsequently fire its ItemCommand server event.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Alfredo Correa
Top achievements
Rank 1
answered on 23 Jul 2009, 12:20 PM
Sebastian,

The Viewstate for both the page and the data grid should be enabled by default but I programmatically enabled them just in case.

Datagrid:  MyBase.EnableViewState = True

Page:  EnableViewState="true"

The ItemCommand is still not being fired. The call goes to the object after I hit the ImageButton. I have set up a break point in the custom control’s Load event that verifies the call is being made.

Alfredo

0
Sebastian
Telerik team
answered on 23 Jul 2009, 12:49 PM
Hi Alfredo,

I conclude that your implementation follows the conventions for RadGrid programmatic creation inside OnInit from the documentation.

Is it possible for you to perform a quick test - extend the MS GridView control and wrap the same image button inside a template field which is build dynamically to see whether the OnRowCommand event of the GridView is fired as expected? If so, I suggest you post a simple demo which hosts both grids (RadGrid and MS GridView) attached to a formal support ticket. I will examine it in detail and will get back to you with more info on the subject.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Alfredo Correa
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Alfredo Correa
Top achievements
Rank 1
Share this question
or