By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

Detect which ImageButton caused PostBack

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:uf**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2

P: n/a
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.
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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.
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:uf**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #3

P: n/a
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.

"Joe" wrote:
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

Nov 18 '05 #4

P: n/a
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
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.

"Joe" wrote:
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

Nov 18 '05 #5

P: n/a
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.

"Joe" wrote:
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
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.

"Joe" wrote:
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


Nov 18 '05 #6

P: n/a
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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.

"Joe" wrote:
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
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.

"Joe" wrote:

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


Nov 18 '05 #7

P: n/a

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:uf**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #8

P: n/a
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

"Joe" wrote:
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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.

"Joe" wrote:
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
> 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.
>
> "Joe" wrote:
>
> > 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
> >
> >
> >


Nov 18 '05 #9

P: n/a
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

Joe wrote:
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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
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.

"Joe" wrote:

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.

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:1E**********************************@micro soft.com...

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.

"Joe" wrote:
>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
>
>
>


Nov 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.