Detect which ImageButton caused PostBack

Discussion in 'ASP .Net' started by Joe, Nov 8, 2004.

  1. Joe

    Joe Guest

    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?

    Joe, Nov 8, 2004
    1. Advertisements

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

    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.
    Steve C. Orr [MVP, MCSD], Nov 8, 2004
    1. Advertisements

  3. Joe

    Joe Guest

    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.
    Joe, Nov 8, 2004
  4. Joe

    Guest Guest


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

    Guest, Nov 8, 2004
  5. Joe

    Joe Guest

    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.
    Joe, Nov 8, 2004
  6. Joe

    Guest Guest


    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.

    Guest, Nov 8, 2004
  7. Joe

    Joe Guest

    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.
    Joe, Nov 8, 2004
  8. Joe

    vMike Guest

    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

    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
    vMike, Nov 8, 2004
  9. Joe

    Guest Guest

    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.

    Guest, Nov 9, 2004
  10. 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.

    Benjamin Gavin, Nov 9, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.