Programming 笔记

工作中遇到的问题就记载这里

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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.

posted on 2005-07-15 16:41  IT 笔记  阅读(397)  评论(0编辑  收藏  举报