473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

AddHandler question

Joe
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
Nov 19 '05 #1
2 1877
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" <jo******@donotspam.yahoo.com> wrote in message
news:BF**********************************@microsof t.com...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

Nov 19 '05 #2
JJ
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.ToU pper)

'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" <jo******@donotspam.yahoo.com> wrote in message
news:BF**********************************@microsof t.com...
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


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: EMW | last post by:
I use the following code in the POPUP event of a contextmenu: mnuItem = New MenuItem mnuItem.Text = "Shelter" cmThs.MenuItems.Add(mnuItem) AddHandler mnuItem.Click, AddressOf MenuItemClicked...
3
by: Jeffrey A. Voigt | last post by:
Can someone take a quick glace at my code and tell me why my AutoPostBackHandler function does not get fired off at all? What I'm trying to do is get all of the Buttons and DropDownList controls...
4
by: DJ | last post by:
Good morning, Still new at this so please bear with me. I am creating a table dynamically using webcontrols based on the output of a sproc from my database.The table represents test instances...
2
by: Just Me | last post by:
When a document is to be printed I call a method that contains an AddHandler statement. I just realized that if a second copy is to be printed the method is called and the AddHandler is executed...
1
by: EMW | last post by:
I use the following code in the POPUP event of a contextmenu: mnuItem = New MenuItem mnuItem.Text = "Shelter" cmThs.MenuItems.Add(mnuItem) AddHandler mnuItem.Click, AddressOf MenuItemClicked...
3
by: hartley_aaron | last post by:
Hi, I was trying to store the address of the my current handler for a particular event so as to simplify using AddHandler and RemoveHandler throughout my code. However, I cannot seem to get any...
12
by: Tom | last post by:
I use dynamically created controls all the time. I.E. I create the control in code then use AddHandler to add the necessary delegates for processing (like Click, etc). Does one have to call...
15
by: Nathan Sokalski | last post by:
I have a section of my code that dynamically creates LinkButtons to allow the user to go to the page containing a question they have not answered. The code that creates the LinkButton is called, as...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.