datagrid 事件不能工作. (Why sometimes datagrid event cannot be fired?)

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.

转载于:https://www.cnblogs.com/yangbin990/archive/2005/07/15/193526.html

你可能感兴趣的:(datagrid 事件不能工作. (Why sometimes datagrid event cannot be fired?))