微软推荐使用DataSource控件,并使用DataSourceID指定数据源控件(似乎也未说明理由,至少我没有看到过)。但很多人并不认同,我一直感觉DataSource控件是一个“黑箱”。

除了ObjectDataSource不够灵活,使用反射,对性能会有所损耗外,我想它还存在下面的这个问题。

 

我以前曾在csdn上问过这个问题:数据源控件读入数据和数据绑定发生在页面事件的哪一个阶段 ,但答案不是很令我满意。

今天我进行了一些测试,并归纳如下,如有缪误,还望各位指出。

 

1、数据源控件的绑定应分“页面初次(Raw)加载” 和“页面回发(postback)”两种情况讨论。

2、在页面初次加载时,数据源绑定应发生在PreRender之后,Render之前。通过设置断点或开启Trace功能可以推断出来。

aspx 代码:

<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:DropDownList ID="DropDownList1" runat="server"
            DataSourceID="ObjectDataSource1" DataValueField="Name">
        
</asp:DropDownList>
        
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="LifeCircle.DataManager" 
            SelectMethod="GetUsers" DataObjectTypeName="LifeCircle.User" 
            InsertMethod="AddUser" >
        
</asp:ObjectDataSource>
        
<br />
        
<asp:Button runat="server" ID="btn" Text="AspButton" onclick="btn_Click" />
        
<br />
        
<input type="submit" value="HtmlButton" /></div>
    
</form>
</body>

相关文章:

  • 2022-12-23
  • 2021-05-25
  • 2022-03-05
  • 2021-06-23
  • 2021-08-30
  • 2021-07-09
猜你喜欢
  • 2021-05-29
  • 2021-11-11
  • 2021-11-23
  • 2022-12-23
相关资源
相似解决方案