开发者

ObjectDataSource firing twice, or on its own

Can someone explain exactly how/when an ObjectDataSource fires? I have an ASP.NET page, with a GridView, which is referencing an ODS. I put a breakpoint in the method the ODS is using, and noticed it was firing twice.

I looked into the code and the a开发者_运维百科nswer seemed obvious at first. I had

    Page_Load()
    {
      if(!Page.IsPostBack)
      {
          MethodA();
          MethodB();
      }
    }

where MethodA and MethodB were both eventually calling gv.DataBind(). This made sense because I assume that each call to GridView.DataBind() would result in asking the ODS for data, and therefore running my data access method.

The weird thing is that when comment out the call to MethodA, it still fires twice. Checking the call stack shows the method being run first as a result of MethodB, and then again, with no trail except [External Code]. This mystery load does not happen when I let MethodA and MethodB both execute.

Any idea what's going on here? Any idea what other code I might have that is asking the ODS for data? I'm starting to think all these 'no code' data controls are more obfuscation and BS than they're worth.


I've experienced this problem when we were hiding/showing gridview column dynamically in code.

Here is a page that talks about some issues that might cause multiple Selects http://forums.asp.net/t/1161164.aspx


Multiple calls to a gridview's databind method can occur implicitly if you're changing the visibility, i.e., showing and/or hiding, the columns of a gridview tied to an object data source.

In this scenario, try encapsulating the show/hide gridview column code in the Page_LoadComplete event handler.

The Page_LoadComplete event handler is in a prime position in the page lifecycle to prevent multiple databind calls and still be effective as it is invoked after control change events (e.g., button click, drop down selected index changed, etc.) yet before the gridview databinding events.


"when comment out the call to MethodA, it still fires twice". So it will likely be Page_Load called twice. Probably you have AutoEventWireup="true" and also registering event in code http://www.aspdeveloper.net/tiki-index.php?page=ASPFaqEventsDoubleFire


If you set the datasource of the gridview with something like

gv.DataSourceID=dsObjDataSource;

then the grid view calls gv.DataBind() on its own.


I had the same problem - the problem was that I was hiding/showing a column after, or during, databinding. Moving the hide/show code before the databinding stopped the binding from happening twice, which I suspect is the same effect as moving it to the Page_Load. In my case the databind was happening in response to a dropdown listbox change - doing the column add/remove before the DataBind() call fixed the twofer problem for me.


I was getting the same result with DataBinding occuring twice using asp:DataList and asp:ObjectDataSource.

It turned out to be because I was using a UserControl in the select parameters:

<asp:ControlParameter Name="GroupID" Type="Int32" DefaultValue="-1"
    ControlID="UserControl1" PropertyName="SelectedGroupID" />

I am getting weary of UserControls. I can see how they might be productivity enhancement for 5th graders but they are a complete waste of time at a higher level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜