AddHandler question

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9l?=, Oct 26, 2005.

  1. I have a datgrid with a button column that contains link buttons. I have
    added code to the ItemDataBound event of the datagrid that adds a handler to
    each link button. Here is the code:

    Private Sub dgForms_ItemDataBound(ByVal sender As System.Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgForms.ItemDataBound

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then

    Dim lb As LinkButton = CType(e.Item.Cells(1).Controls(0), LinkButton)
    AddHandler lb.Click, AddressOf RetrieveformIdClickHandler
    lb = Nothing
    End If

    End Sub

    The RetrieveformIdClickHandler Sub follows:

    Private Sub RetrieveformIdClickHandler(ByVal sender As System.Object, ByVal
    e As System.EventArgs)
    Dim lb As LinkButton = CType(sender, LinkButton)
    m_FormId = lb.Text
    lb = Nothing
    End Sub

    When I click on one of the link buttons after it has been rendered, this
    event handler never fires. Is there some obvious reason why? Can any of you
    see womething glaringly wrong with what I've written?

    FYI, page-level ViewState is disabled on each page at this client site.
    They don't allow its use.

    TIA,
    --
    Joe

    VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
     
    =?Utf-8?B?Sm9l?=, Oct 26, 2005
    #1
    1. Advertising

  2. Hi Joe,

    > When I click on one of the link buttons after it has been rendered, this
    > event handler never fires. Is there some obvious reason why? Can any of
    > you
    > see womething glaringly wrong with what I've written?


    Yes. You've declared the button inside a function. It lives for the lifetime
    of the function, and then disappears into the void. In addition, as a Page
    class has a lifetime of precisely ONE Request, you will need to add the
    buttons back in to the Page with each PostBack, and scope them to the Page.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    A watched clock never boils.

    "Joe" <> wrote in message
    news:...
    >I have a datgrid with a button column that contains link buttons. I have
    > added code to the ItemDataBound event of the datagrid that adds a handler
    > to
    > each link button. Here is the code:
    >
    > Private Sub dgForms_ItemDataBound(ByVal sender As System.Object, ByVal e
    > As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > dgForms.ItemDataBound
    >
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > ListItemType.AlternatingItem Then
    >
    > Dim lb As LinkButton = CType(e.Item.Cells(1).Controls(0), LinkButton)
    > AddHandler lb.Click, AddressOf RetrieveformIdClickHandler
    > lb = Nothing
    > End If
    >
    > End Sub
    >
    > The RetrieveformIdClickHandler Sub follows:
    >
    > Private Sub RetrieveformIdClickHandler(ByVal sender As System.Object,
    > ByVal
    > e As System.EventArgs)
    > Dim lb As LinkButton = CType(sender, LinkButton)
    > m_FormId = lb.Text
    > lb = Nothing
    > End Sub
    >
    > When I click on one of the link buttons after it has been rendered, this
    > event handler never fires. Is there some obvious reason why? Can any of
    > you
    > see womething glaringly wrong with what I've written?
    >
    > FYI, page-level ViewState is disabled on each page at this client site.
    > They don't allow its use.
    >
    > TIA,
    > --
    > Joe
    >
    > VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
     
    Kevin Spencer, Oct 26, 2005
    #2
    1. Advertising

  3. Hi Kevin,

    Then why does this work?

    Private Sub PopulateCategoryTable()

    'Retrieve Category values from database
    Dim CForm As New ClaimForm
    'populate datatable
    dtCategories = CForm.LoadCategories(hdnLineOfBusniessID.Value.ToUpper)

    'Add 'View All Categories' option
    Dim r As DataRow
    r = dtCategories.NewRow()
    r("CATEGORY_CODE") = "VA"
    r("DESCRIPTION") = "View All Categories"
    dtCategories.Rows.Add(r)
    r = Nothing

    For Each r In dtCategories.Rows
    Dim tr As New TableRow
    Dim tc As New TableCell

    Dim lb As New LinkButton
    lb.Text = CType(r.Item(1), String).ToUpper
    lb.ID = CType(r.Item(0), String).ToUpper

    AddHandler lb.Click, AddressOf ConnectCategoryLinks

    tc.CssClass = "tabledata"
    tc.Controls.Add(lb)
    tr.Cells.Add(tc)
    tblCategory.Rows.Add(tr)

    lb = Nothing
    tc = Nothing
    tr = Nothing
    Next

    CForm = Nothing
    End Sub

    Where

    Private Sub ConnectCategoryLinks(ByVal sender As System.Object, ByVal e
    As System.EventArgs)
    Dim tr As TableRow
    Dim lb As LinkButton = CType(sender, LinkButton)
    hdnSelectedCategory.Value = lb.ID

    'Disable the selected category link button
    For Each tr In tblCategory.Rows
    CType(tr.Cells(0).Controls(0), LinkButton).Enabled = _
    Not (String.Equals(CType(tr.Cells(0).Controls(0),
    LinkButton).ID, lb.ID))
    Next

    lb = Nothing
    'populate datagrid
    PopulateClaimFormsDataGrid(hdnSortField.Value, hdnSortOrder.Value)
    End Sub


    I'm doing the exact same thing - declaring the button and adding the
    handler. I don't see any difference, other than the code above declares and
    adds the button to a table row and the code that I sent yesterday retrieves a
    reference to the button in a datagrid. So what differnce does it make?

    Thanks,

    Joe

    "Kevin Spencer" wrote:

    > Hi Joe,
    >
    > > When I click on one of the link buttons after it has been rendered, this
    > > event handler never fires. Is there some obvious reason why? Can any of
    > > you
    > > see womething glaringly wrong with what I've written?

    >
    > Yes. You've declared the button inside a function. It lives for the lifetime
    > of the function, and then disappears into the void. In addition, as a Page
    > class has a lifetime of precisely ONE Request, you will need to add the
    > buttons back in to the Page with each PostBack, and scope them to the Page.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > ..Net Developer
    > A watched clock never boils.
    >
    > "Joe" <> wrote in message
    > news:...
    > >I have a datgrid with a button column that contains link buttons. I have
    > > added code to the ItemDataBound event of the datagrid that adds a handler
    > > to
    > > each link button. Here is the code:
    > >
    > > Private Sub dgForms_ItemDataBound(ByVal sender As System.Object, ByVal e
    > > As
    > > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > > dgForms.ItemDataBound
    > >
    > > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    > > ListItemType.AlternatingItem Then
    > >
    > > Dim lb As LinkButton = CType(e.Item.Cells(1).Controls(0), LinkButton)
    > > AddHandler lb.Click, AddressOf RetrieveformIdClickHandler
    > > lb = Nothing
    > > End If
    > >
    > > End Sub
    > >
    > > The RetrieveformIdClickHandler Sub follows:
    > >
    > > Private Sub RetrieveformIdClickHandler(ByVal sender As System.Object,
    > > ByVal
    > > e As System.EventArgs)
    > > Dim lb As LinkButton = CType(sender, LinkButton)
    > > m_FormId = lb.Text
    > > lb = Nothing
    > > End Sub
    > >
    > > When I click on one of the link buttons after it has been rendered, this
    > > event handler never fires. Is there some obvious reason why? Can any of
    > > you
    > > see womething glaringly wrong with what I've written?
    > >
    > > FYI, page-level ViewState is disabled on each page at this client site.
    > > They don't allow its use.
    > >
    > > TIA,
    > > --
    > > Joe
    > >
    > > VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation

    >
    >
    >
     
    =?Utf-8?B?Sko=?=, Oct 27, 2005
    #3
  4. =?Utf-8?B?Sm9l?=

    leeeyre

    Joined:
    Sep 21, 2008
    Messages:
    2
    Location:
    Northamptonshire
    I think if you have a control inside a gridview then the gridview row command will fire instead of the button. You can give your button a command name and catch the event in the gridviews row command with an if e.commandname = .
     
    leeeyre, Jul 8, 2010
    #4
    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. Jeffrey A. Voigt

    Help w/AddHandler (Not Firing Off)

    Jeffrey A. Voigt, Aug 10, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    675
    Natty Gur
    Aug 11, 2003
  2. Natty Gur

    Re: Help w/AddHandler (Not Firing Off)

    Natty Gur, Aug 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    Jeff Voigt
    Aug 11, 2003
  3. Kay

    Event AddHandler

    Kay, Feb 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,202
  4. Craig Buchanan

    AddHandler question

    Craig Buchanan, Feb 12, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    408
    Kevin Spencer
    Feb 12, 2004
  5. =?Utf-8?B?Sm9l?=

    AddHandler question

    =?Utf-8?B?Sm9l?=, Mar 6, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    393
    =?Utf-8?B?Sm9l?=
    Mar 6, 2006
Loading...

Share This Page