AddHandler question

G

Guest

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,
 
K

Kevin Spencer

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.
 
G

Guest

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 said:
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.
 
Joined
Sep 21, 2008
Messages
2
Reaction score
0
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 = .
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top