Detect which ImageButton caused PostBack

J

Joe

I have a DataGrid with a templated column that displays ImageButtons. I need
to know if one of these buttons caused the postback or just another button
on the form. If one of these buttons caused the postback than I don't want
to do a DataBind otherwise I do.

I know __EVENTTARGET doesn't return this information. Is there anything else
that does?

Thanks,
Joe
 
S

Steve C. Orr [MVP, MCSD]

You can override the RaisePostBackEvent method of the Page class.
The first parameter sent to this method is the object that caused the
postback.
Remember to call MyBase.RaisePostBackEvent so the postback event gets raised
correctly.

Alternately, instead of the load event you could move your conditional code
to the PreRender event , which happens after all the control events. So by
then you'll know which control(s) caused the postback.

If you can be sure the control that caused the postback is a button, From
within your Page_Load event you can look for the names of the buttons in the
Request.Form collection.
If the button name is present then you know that button caused the postback.
 
J

Joe

Thanks Steve but it doesn't work exactly right.

I used the RaisePostBackEvent to set a flag if am ImageButton in the grid
was clicked (this part is fine) Then in the PreRender, I check the flag to
see if I should call BindData or not (this part sort of works).

What is happening now is when the data should be reloaded and re-bound it
seems to be too late and the DataGrid doesn't get effected.
 
G

Guest

Hi

you can put on your button an commandName. On itemCommand check if the
command is your command.

let's say CommanName = "Insert"
if (e.CommandName == "Insert")

Cheers.
 
J

Joe

I don't get the ItemCommand event because my DataGrid is being bound to
before the event can be called. I need to be able to determine if one of
these ImageButtons caused the postback so I don't do the bind.
 
G

Guest

Hi

this mean that you are doing data binding to earlier, probably on your
page_load. You should put your databinding on your grid prerender event. This
will make first ItemCommand event if any and after this will make prerender
and your binding.

Cheers.
 
J

Joe

I had tried that. See me previous response to Steve.

If I bind in the prerender event, the datagrid doesn't change. It seems like
it's too late.
 
V

vMike

Joe said:
I have a DataGrid with a templated column that displays ImageButtons. I need
to know if one of these buttons caused the postback or just another button
on the form. If one of these buttons caused the postback than I don't want
to do a DataBind otherwise I do.

I know __EVENTTARGET doesn't return this information. Is there anything else
that does?

Thanks,
Joe
I use this to get the index of the image button clicked.
You could use it to test for an image button click

Sub butDelete_Click(sender as object, e As ImageClickEventArgs) handles
butDelete.click

dim strSender as string = DirectCast(sender,imagebutton).UniqueID
dim strArray() as string = split(strSender,":")
dim i as int32 = strArray.getupperbound(0)
dim intIndex as int32 =
ctype(strArray(i-1).substring(strArray(i-1).indexof("_ctl") + 4),int32) - 1
 
G

Guest

So
If two people said to you that you should use prerender to bind, seem that
this event is not too late. In all my forms i bind my grid on grid_prerender
event and it's really working very fine.
So, you should ask what is happening? try to make a simple example to see
that this thing is working and then try to apply on your form.

Cheers
 
B

Benjamin Gavin

Have you tried, for instance, not binding the grid at all in the Load
event if the page is in postback mode? If you do this [and the grid has
viewstate enabled] you should be able to properly capture the
ItemCommand event. You can then re-bind the grid in the ItemCommand
handler, or in PreRender handler as necessary.

You can also "convince" the ASP.NET engine to fire the ItemCommand event
if you can manage to reconstruct the datagrid control tree _exactly_ as
it was on the immediately previous call. This is rather difficult, and
I don't know that I've ever gotten it to work precisely correct. In
either case, you're probably better off just enabling viewstate for the
data grid, or overridding the default behavior of the image button
control to include a client-side onclick handler which sends a
pre-defined __EVENTARGUMENT which is something you can parse and handle
in your Load event handler.

Ben
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top