LinkButton / doPostBack problem

R

Rob Meade

Hi all,

I have a bit of code that programmatically adds some table cells/rows to a
table.

The last cell adds a LinkButton control, I call it like this:

Cell.Controls.Add(deleteRecipientLink(objReader("NotificationID")))

I feed it with an ID from my database table.

The function 'deleteRecipientLink' is as follows:

Private Function deleteRecipientLink(ByVal recipientID As Integer) As
LinkButton

' declare variables
Dim link As LinkButton

' create new instance of linkbutton object
link = New LinkButton

' set link properties
link.ID = "deleteRecipient" & recipientID
link.CssClass = "normalText"
link.Text = "Delete"
link.ToolTip = "Delete"
link.CausesValidation = False
link.CommandArgument = recipientID
link.CommandName = "recipientID"

' add handler
AddHandler link.Click, AddressOf deleteRecipient

' return link object

Return (link)

End Function

The problem I'm having is that whilst the link button is not validating my
form (correct), it is obviously doing a post back, which in my case is now
adding more rows to my data (as if the form had been submitted correctly),
what I want to happen is run a different function when the link button is
clicked. I thought adding the AddHandler would have solved this problem but
having stuffed in some breakpoints it never gets to my deleteRecipient
function - it hits the PageIsPostback and then thats it.

What do I need to do? Change my code for the AddHandler some how, or change
my code in the if its a PostBack? if the latter, how do I determine that it
was the linkbutton that did the postback and not the submit button on my
form?

Thanks in advance for any help.

Regards

Rob
 
R

Raterus

Controls aren't persisted across postbacks, so if you want that event to fire, you need to recreate the linkbutton just like you did the first time in page_load.
 
R

Rob Meade

...
Controls aren't persisted across postbacks, so if you want that event to
fire, you need to recreate the linkbutton just like you did the first time
in page_load.

Hi Raterus,

Thanks for your reply.

A slightly ruder version of "oh crap" just resinated around my office upon
reading your reply :eek:/

So, where at the moment I run off to my database, get a list of users, and
add the linkbutton control to 'delete' each one, I'd need to run that on the
post back aswell so that the controls exist for the event to then fire?

Correct?

Regards

Rob
 
R

Raterus

That is correct, as much as it's a pain. If you can possibly get the same functionality by using a datagrid/datalist/repeater I'd go that route, as you won't need to do any of this, just handle the event. I remember going through all of this many months ago when I had a similar "delete" problem, after doing what I originally suggested, I then had the problem that I just recreated the very user I wanted to delete. I ended up using a datagrid and all was better.

Hope you can work it out, remember dynamic controls w/ events can be a pain, avoid them if you possibly can!

--Michael
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top