DataGrid Event fun with Linkbuttons and Pushbuttons...

Published 04 April 03 02:43 PM

Well, there have been many questions regarding why in a datagrid the ItemCommand does not fire when using Push Buttons, but when using LinkButtons, everything works as expected.

So here is the skinny...

When using a LinkButton, you see in the Status Bar the following:

javascript: __doPostBack('Northwind:_ctrl1:_ctrl3','');

This causes the following to happen:

  theform.__EVENTTARGET.value = eventTarget;
  theform.__EVENTARGUMENT.value = eventArgument;

With this in place, the Eventtarget picked up by the CLR on PostBack, raises the PostBack Event and will fire the event even if you Re-bind over the DataGrid.  This seems to work fine no matter what because of this javascript function.

When you use the PushButton, it is merely a submit button and the name of the submit button would be the 'Northwind:_ctrl1:_ctrl3' so that the event target would still be there, but it would not get put in place until the events were processed.  So in this case if you Re-Bind your Datagrid in your Page_Load then it overwrites the event and it does not fire.

This is the great reason that when you are doing PostBack and you have a Binding Process that happens in the Page_Load, make sure you test for PostBack before you rebind. 

C#:
if(!IsPostBack) {
  //  do something
}

VB:
If Not IsPostBack Then
  ' do something
End If

Then you will not have problems with your PostBack events with a DataGrid.

Happy DataGridding....

Comments

# TrackBack said on April 4, 2003 2:43 PM:
Ben Miller stole my line! : Datagrid Girl
# Vivek Vaid said on August 27, 2003 8:18 AM:
In addtion, remember to check that your enableViewState is set to "true" else your ItemCommand event won't fire - even for a LinkButton.
# Rajkumar said on October 1, 2003 4:09 AM:
i have a javascript: __doPostBack('dtgPick:_ctrl1:_ctrl3','') for a link button in the edit item of a datagrid.The event doesnt fire.I have checked for postback in the Page_Load.
Can you help me out???
# Ben Miller said on October 1, 2003 9:25 AM:
Feel free to email me at benm@REMOVETHISduckpond.com and just take out the REMOVETHIS and I will be happy to take this one offline.
# Tolga said on February 3, 2004 7:35 AM:
Ben,
I have a problem with the events as well. My grid is inserted at run-time to a control that is inserted at run-time to a page. If I don't rebind, then my controls don't appear during post-back. So is there no way for me to use the datagrid edit? The funny part is, my "Edit" button event fires OK. My "Update" and "Cancel" events don't fire.

Your help is greatly appreciated. You can e-mail me at tercan_at_REMOVETHISdoe.state.in.us.
# RichB said on June 30, 2004 2:20 PM:
Ben,

Thanks for the help. I searched all over the web as to why I couldn't get the ItemCommand event to fire. This actually explained it instead of just throwing a bunch of someone's code at me.

RichB
# ASIF said on July 13, 2004 8:29 AM:
HI GUYSS...PLZZZ HELP..WHEN I RUN MY PROJECT..ENTER THE VALUES INTO THE TEXTBOXES FOR ADDING THE RECORD..ADN THEN I PRESS THE BUTTON....

PROBLEM COMES HERE WHEN I PRESS THE BUTTON IT CAN ONLY ENTER THE VALUES ONCE TO THE DATABASE ..WHEN I ADD SECOND RECORD IT DO NOT ENTER AND SHOWSS ME THE PREVIOUS VALUES WHICH I ENTERED JUST BEFORE....


PLZZZZZZZ HELP ME...I M IN HUGE TROUBLEE :( :(
# Musa said on July 21, 2004 12:46 AM:
Vivek Vaid made the big shot for me. His note solved my problem even though it is simple but it is a MUST. I was hitting the wall for 6 hours to figure out the probloem. Finally this page solved all of my problems. GREAT JOB folks. Keep it UP.
# abtcabe said on August 4, 2004 3:30 PM:
Thx for this !!!!!!!!!!
# An OTORI said on June 12, 2007 11:51 AM:

Uhhhh yeeeeeeeaaaaaaaaaahhhhhhhhhh....you saved my evening :)

thx man

Anonymous comments are disabled
Page view tracker