ASP.NET 自定义对象数据源
我试图通过继承 System.Web.UI.WebControls.ObjectDataSource 来编写自定义 DataSource 控件。这是我的数据源类的代码。
public class MyDataSource : ObjectDataSource
{
public MyDataSource()
{
this.TypeName = GetType().FullName;
this.SelectMethod = "SelectAll";
this.SelectCountMethod = "SelectCount";
}
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public DataTable SelectAll()
{
// Do something using this.DataObjectTypeName
DataTable dt = new DataTable();
// Fill DataTable
return dt;
}
public int SelectCount()
{
// Here is not important yet
return 20;
}
}
我使用数据源的方式如下:
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="true" Width="100%"
AllowPaging="true" AllowSorting="true" PageSize="10" DataSourceID="myDataSource">
</asp:GridView>
<cc2:MyDataSource ID="myDataSource" runat="server"
DataObjectTypeName="MyLib.MyClass, MyLib">
</cc2:MyDataSource>
我的 aspx.cs 文件中没有编写任何代码,如果我在 SelectAll 方法中编写硬编码的选择逻辑,我的代码可以正常工作。但是,当我尝试在 SelectAll 方法中使用 DataObjectTypeName 属性时,我发现该属性具有空字符串值。我在数据源代码中放置了四个断点。第一个在构造函数中,第二个在 OnInit 中,第三个在 OnLoad 中,最后一个在 SelectAll 方法中。以下是我运行项目时代码停止的位置和 DataObjectTypeName 值的列表。
1) @Constructer: DataObjectTypeName = "" 2) @OnInit : DataObjectTypeName = "MyLib.MyClass, MyLib" 3) @OnLoad : DataObjectTypeName = "MyLib.MyClass, MyLib" 4) @Constructer: DataObjectTypeName = "" 5) @SelectAll : DataObjectTypeName = ""
Q1:为什么构造函数会被调用两次?
Q2:为什么第二次调用构造函数后没有分配 DataObjectTypeName 属性?
预先感谢,
穆罕默德。
I am trying to write a custom DataSource control by inheriting System.Web.UI.WebControls.ObjectDataSource. Here is my code for my data source class.
public class MyDataSource : ObjectDataSource
{
public MyDataSource()
{
this.TypeName = GetType().FullName;
this.SelectMethod = "SelectAll";
this.SelectCountMethod = "SelectCount";
}
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
[DataObjectMethod(DataObjectMethodType.Select)]
public DataTable SelectAll()
{
// Do something using this.DataObjectTypeName
DataTable dt = new DataTable();
// Fill DataTable
return dt;
}
public int SelectCount()
{
// Here is not important yet
return 20;
}
}
I use my data source as follows:
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="true" Width="100%"
AllowPaging="true" AllowSorting="true" PageSize="10" DataSourceID="myDataSource">
</asp:GridView>
<cc2:MyDataSource ID="myDataSource" runat="server"
DataObjectTypeName="MyLib.MyClass, MyLib">
</cc2:MyDataSource>
No code is written in my aspx.cs file and my code works fine if I write a hardcoded select logic in SelectAll method. But when I tried to use DataObjectTypeName property in SelectAll method I saw that this property has an empty string value. I put four break points in my data source code. First in the constructer, second in OnInit, third in OnLoad and last in SelectAll methods. Here is the list of where code has stopped and values of DataObjectTypeName when I run the project.
1) @Constructer: DataObjectTypeName = "" 2) @OnInit : DataObjectTypeName = "MyLib.MyClass, MyLib" 3) @OnLoad : DataObjectTypeName = "MyLib.MyClass, MyLib" 4) @Constructer: DataObjectTypeName = "" 5) @SelectAll : DataObjectTypeName = ""
Q1: Why constructer is invoked twice?
Q2: Why DataObjectTypeName property is not assigned after second invocation of constructer?
Thanks in advance,
Mehmet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是从
ObjectDataSource
继承只需按预期使用它,并将所有登录信息打包到一个单独的对象(另一个类)中
ObjectDataSource
将实际使用。阅读有关该类的 MSDN 文档,它非常强大并且允许
对于无限的可能性...
Q1)对于你的问题,构造函数被调用两次,因为 ObjectDataSource 正在尝试实例化自身(这就是当你提供 TypeName 时它所做的)
Q2)回答一个解释了你的第二个问题。
Instead of Inheriting from the
ObjectDataSource
just use it as it was intended and pack all your login into a seperate object (another class)
that the
ObjectDataSource
will actually use.Read the MSDN documentation about the class, it is quite robust and allows
for unlimited possibilities...
Q1) And for your question, the constructor is called twice because the ObjectDataSource is trying to instanciate itself (That's what it does when you supply a TypeName)
Q2) Answer one explaings your second question.