Web Custom Control - Datagrid Event Handling Problem

Discussion in 'ASP .Net' started by The Alchemist, Jun 7, 2004.

  1. I am having a problem with a dynamically-generated Datagrid. It is
    important to point out that this problem does not exist with a
    design-time created Datagrid, but only with a dynamically generated
    Datagrid in a Web Custom Control (WCC) :

    The datagrid has LinkButton Column which has a select LinkButton for
    each row. When this button is clicked, the Datagrid raises its
    'ItemCommand' event which captures the information for that row and
    sends it to a event handler method. The problem is that this method is
    never getting called. It seems logical to assume that there is
    something wrong with the event-wiring of the datagrid. The signature
    of the event-handler is:

    Private Sub DataGrid1_SelectedIndexChanged(ByVal source As Object,
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    dg.ItemCommand

    (dg is the name of the datagrid). The 'Handles' keyword in VB.Net is
    supposed to wire the event to the handler but that is not happening.
    So I tried to manually add the handler to the event with the following
    line of code in the CreateChildControls method of the WCC using the
    line:

    AddHandler dg.ItemCommand, AddressOf Me.DataGrid1_SelectedIndexChanged

    That does not work either. The datagrid loads up just fine, and on
    selecting a row it does a PostBack and hits the OnLoad event but never
    hits the event-handler method.

    Why is this event not being wired to the handler? Or might there be
    some other problem?

    Thanks in advance for any help.



    BZ
    The Alchemist, Jun 7, 2004
    #1
    1. Advertising

  2. The Alchemist

    Raterus Guest

    Please don't crosspost, this post has no business in these forums as it has nothing to do with VB or VB's controls.

    microsoft.public.dotnet.languages.vb,
    microsoft.public.dotnet.languages.vb.controls

    I'll be nice though and answer your question, you haven't re-instantiated your datagrid in page_load of the postbacked page, and given it the same ID as the previous page had.

    "The Alchemist" <> wrote in message news:...
    > I am having a problem with a dynamically-generated Datagrid. It is
    > important to point out that this problem does not exist with a
    > design-time created Datagrid, but only with a dynamically generated
    > Datagrid in a Web Custom Control (WCC) :
    >
    > The datagrid has LinkButton Column which has a select LinkButton for
    > each row. When this button is clicked, the Datagrid raises its
    > 'ItemCommand' event which captures the information for that row and
    > sends it to a event handler method. The problem is that this method is
    > never getting called. It seems logical to assume that there is
    > something wrong with the event-wiring of the datagrid. The signature
    > of the event-handler is:
    >
    > Private Sub DataGrid1_SelectedIndexChanged(ByVal source As Object,
    > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    > dg.ItemCommand
    >
    > (dg is the name of the datagrid). The 'Handles' keyword in VB.Net is
    > supposed to wire the event to the handler but that is not happening.
    > So I tried to manually add the handler to the event with the following
    > line of code in the CreateChildControls method of the WCC using the
    > line:
    >
    > AddHandler dg.ItemCommand, AddressOf Me.DataGrid1_SelectedIndexChanged
    >
    > That does not work either. The datagrid loads up just fine, and on
    > selecting a row it does a PostBack and hits the OnLoad event but never
    > hits the event-handler method.
    >
    > Why is this event not being wired to the handler? Or might there be
    > some other problem?
    >
    > Thanks in advance for any help.
    >
    >
    >
    > BZ
    Raterus, Jun 7, 2004
    #2
    1. Advertising

  3. "Raterus" <> wrote in message news:<u#>...
    > Please don't crosspost, this post has no business in these forums as it
    > has nothing to do with VB or VB's controls.
    >
    > microsoft.public.dotnet.languages.vb,
    > microsoft.public.dotnet.languages.vb.controls


    This control is written in VB.Net, how can it not be relevant to these
    groups?

    >
    > I'll be nice though and answer your question, you haven't
    > re-instantiated your datagrid in page load of the postbacked page, and
    > given it the same ID as the previous page had.


    Yes I did and it does not not work.

    I am posting the code below, any help would be MUCH appreciated.


    Imports System.ComponentModel
    Imports System.Web.UI
    Imports System.web.UI.WebControls

    <DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1
    runat=server></{0}:WebCustomControl1>")> Public Class
    WebCustomControl1
    Inherits System.Web.UI.WebControls.WebControl

    Protected WithEvents dg As DataGrid


    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)

    Dim DataTable As New DataTable

    Dim col As New DataColumn
    col.ColumnName = "Last Name"
    DataTable.Columns.Add(col)
    Dim col2 As New DataColumn
    col2.ColumnName = "First Name"
    DataTable.Columns.Add(col2)
    Dim col3 As New DataColumn
    col3.ColumnName = "Level"
    DataTable.Columns.Add(col3)

    Dim selectColumn As New ButtonColumn
    selectColumn.ButtonType = ButtonColumnType.LinkButton
    selectColumn.CommandName = "select"
    selectColumn.Text = "select"

    dg.Columns.Add(selectColumn)

    Dim row As DataRow
    row = DataTable.NewRow
    row.Item(0) = "Smith"
    row.Item(1) = "John"
    row.Item(2) = "Level 1"
    DataTable.Rows.Add(row)

    row = DataTable.NewRow
    row.Item(0) = "Jones"
    row.Item(1) = "Mary"
    row.Item(2) = "Level 2"
    DataTable.Rows.Add(row)

    dg.DataSource = DataTable
    dg.DataBind()
    dg.RenderControl(output)


    End Sub


    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

    dg = New DataGrid
    dg.BackColor = System.Drawing.Color.Red
    dg.ID = "testID"
    'I tried the following line instead of using the handles
    keyword and that didnt work either:
    'AddHandler dg.ItemCommand, AddressOf
    Me.DataGrid1_SelectedIndexChanged
    Controls.Add(dg)

    End Sub

    'Public Sub bz()

    Private Sub DataGrid1_SelectedIndexChanged(ByVal source As Object,
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    dg.ItemCommand

    Debug.WriteLine("The event handler has been called.")

    End Sub

    End Class







    >
    > "The Alchemist" <> wrote in message
    > news:...
    > > I am having a problem with a dynamically-generated Datagrid. It is
    > > important to point out that this problem does not exist with a
    > > design-time created Datagrid, but only with a dynamically generated
    > > Datagrid in a Web Custom Control (WCC) :
    > >
    > > The datagrid has LinkButton Column which has a select LinkButton for
    > > each row. When this button is clicked, the Datagrid raises its
    > > 'ItemCommand' event which captures the information for that row and
    > > sends it to a event handler method. The problem is that this method is
    > > never getting called. It seems logical to assume that there is
    > > something wrong with the event-wiring of the datagrid. The signature
    > > of the event-handler is:
    > >
    > > Private Sub DataGrid1 SelectedIndexChanged(ByVal source As Object,
    > > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    > > dg.ItemCommand
    > >
    > > (dg is the name of the datagrid). The 'Handles' keyword in VB.Net is
    > > supposed to wire the event to the handler but that is not happening.
    > > So I tried to manually add the handler to the event with the following
    > > line of code in the CreateChildControls method of the WCC using the
    > > line:
    > >
    > > AddHandler dg.ItemCommand, AddressOf Me.DataGrid1 SelectedIndexChanged
    > >
    > > That does not work either. The datagrid loads up just fine, and on
    > > selecting a row it does a PostBack and hits the OnLoad event but never
    > > hits the event-handler method.
    > >
    > > Why is this event not being wired to the handler? Or might there be
    > > some other problem?
    > >
    > > Thanks in advance for any help.
    > >
    > >
    > >
    > > BZ
    The Alchemist, Jun 8, 2004
    #3
  4. "The Alchemist" <> wrote in message
    news:...
    > "Raterus" <> wrote in message

    news:<u#>...
    > > I'll be nice though and answer your question, you haven't
    > > re-instantiated your datagrid in page load of the postbacked page, and
    > > given it the same ID as the previous page had.

    >
    > Yes I did and it does not not work.
    >
    > I am posting the code below, any help would be MUCH appreciated.
    >
    >
    > Imports System.ComponentModel
    > Imports System.Web.UI
    > Imports System.web.UI.WebControls
    >
    > <DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1
    > runat=server></{0}:WebCustomControl1>")> Public Class
    > WebCustomControl1
    > Inherits System.Web.UI.WebControls.WebControl
    >
    > Protected WithEvents dg As DataGrid
    > Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    >
    > dg = New DataGrid
    > dg.BackColor = System.Drawing.Color.Red
    > dg.ID = "testID"
    > 'I tried the following line instead of using the handles
    > keyword and that didnt work either:
    > 'AddHandler dg.ItemCommand, AddressOf
    > Me.DataGrid1_SelectedIndexChanged
    > Controls.Add(dg)
    >
    > End Sub


    How about MyBase.OnLoad(e)
    --
    John Saunders
    johnwsaundersiii at hotmail
    John Saunders, Jun 8, 2004
    #4
  5. Hi,

    I am experiencing the same problem as you are.
    Have you got any solution?

    "The Alchemist" <> wrote in message
    news:...
    > I am having a problem with a dynamically-generated Datagrid. It is
    > important to point out that this problem does not exist with a
    > design-time created Datagrid, but only with a dynamically generated
    > Datagrid in a Web Custom Control (WCC) :
    >
    > The datagrid has LinkButton Column which has a select LinkButton for
    > each row. When this button is clicked, the Datagrid raises its
    > 'ItemCommand' event which captures the information for that row and
    > sends it to a event handler method. The problem is that this method is
    > never getting called. It seems logical to assume that there is
    > something wrong with the event-wiring of the datagrid. The signature
    > of the event-handler is:
    >
    > Private Sub DataGrid1_SelectedIndexChanged(ByVal source As Object,
    > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    > dg.ItemCommand
    >
    > (dg is the name of the datagrid). The 'Handles' keyword in VB.Net is
    > supposed to wire the event to the handler but that is not happening.
    > So I tried to manually add the handler to the event with the following
    > line of code in the CreateChildControls method of the WCC using the
    > line:
    >
    > AddHandler dg.ItemCommand, AddressOf Me.DataGrid1_SelectedIndexChanged
    >
    > That does not work either. The datagrid loads up just fine, and on
    > selecting a row it does a PostBack and hits the OnLoad event but never
    > hits the event-handler method.
    >
    > Why is this event not being wired to the handler? Or might there be
    > some other problem?
    >
    > Thanks in advance for any help.
    >
    >
    >
    > BZ
    Mircea Pleteriu, Jun 23, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson
    Replies:
    1
    Views:
    564
    Scott Allen
    Apr 5, 2005
  2. SammyBar
    Replies:
    1
    Views:
    590
    Erjan Gavalji
    Jul 21, 2009
  3. The Alchemist

    Web Custom Control - Datagrid Event Handling Problem

    The Alchemist, Jun 7, 2004, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    178
    Mircea Pleteriu
    Jun 23, 2004
  4. Jonah Olsson
    Replies:
    2
    Views:
    189
    Jonah Olsson
    Apr 5, 2005
  5. SammyBar

    Custom event from a custom web control is fired twice

    SammyBar, Jul 14, 2009, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    924
    Erjan Gavalji
    Jul 21, 2009
Loading...

Share This Page