Handling a button in repeater

D

Dstl Web Team

Any advice on the following please?

Got a repeater on my page listing records from a database.
I slot a button in the last column which takes the record unique ID as
it's CommandName.
When the button is clicked it calls a function and passes the ID

So:

Sub FuncName(ByVal sender as system.object, ByVal e as
RepeaterCommandEventArgs)
Dim btn as Button
Dim IDdata
IDdata = btn.CommandName

IDdata is then used to query a database to return all the record
details for that unique ID

End FuncName.

I get the error of "Object reference not set to an instance of an
object". I'm definetly pulling a value out because the following test
prints the value to the screen.

TextBox.text = IDdata

Help
Thanks
Kevin
 
A

Alessandro Zifiglio

Dstl, What is button supposed to be ?

You have declared a button, and then you are tyring to access its
commandName attribute. Its normal you are getting that error. First of all
your button is not instantiated. You have only declared a button. To
instantiate it, you should use the new keyword on your button declaration
which will create a new button for you, which is not what you want anyway,
but will get rid of that error.

what you want to do instead is get the CommandName you stored within the
button in your repeater control, and get this CommandName when that button
is clicked, if i'm not wrong and your repeater exposes a few events for this
purpose.

For a click event fired you could use the onItemCommand method and code
here, this will fire for postbacks generated from within your repeater
control.

Sub R1_ItemCommand(Sender As Object, e As RepeaterCommandEventArgs)
dim IDData as string
IDData = CType(e.CommandSource, Button).CommandName
End Sub

or use the onItemCreated method which fires when the items within the
repeater control are created. Or the onItemDataBound method when the
repeater binds to a datasource --
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top