[英]SelectMethod called twice for ObjectDataSource
as I am new to ASP.NET webforms and Entity framework, I am experimenting with a pet project. 因为我是ASP.NET webforms和Entity框架的新手,我正在尝试一个宠物项目。
During this I came across the following which I am trying to understand: 在此期间,我遇到了以下我想要了解的内容:
EmployerObjectDataSource
) that is using a method of business logic layer (BLL) object for selecting the data - the method is GetEmployer
我有一个ObjectDataSource(称为EmployerObjectDataSource
),它使用业务逻辑层(BLL)对象的方法来选择数据 - 方法是GetEmployer
Page_PreRender
callback of my page, I call a method populateFields
to populate the fields within a FormView 在我的页面的Page_PreRender
回调中,我调用方法populateFields
来填充FormView中的字段 populateFields
I call EmployerObjectDataSource.Select()
to get the Employer
record. 在populateFields
我调用EmployerObjectDataSource.Select()
来获取Employer
记录。 Here is the code: 这是代码:
//Following Dmytro's comment, I will use Page_Load instead, however this
//does not resolve the problem
//protected void Page_PreRender(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e)
{
_username = "Lefteris";
_version = 1;
if (!Page.IsPostBack)
{
populateFields();
}
}
private bool populateFields()
{
//IEnumerable<Employer> empl = ((IEnumerable<Employer>)EmployerObjectDataSource.Select()).ToList();
//The GetEmployer method of BLL is called here (as expected)
List<Employer> empl = (List<Employer>)EmployerObjectDataSource.Select();
System.Threading.Thread.Sleep(1000);
if (empl.Count() == 1)
{
Employer employer = empl.First();
//The GetEmployer method of BLL is called here (WHY????)
((RadTextBox)EmployerFormView.Row.FindControl("txtAme")).Text = employer.AME.ToString();
((RadTextBox)EmployerFormView.Row.FindControl("txtAfm")).Text = employer.EmplrAFM.ToString();
((RadTextBox)EmployerFormView.Row.FindControl("txtName")).Text = employer.EmplrLastName.ToString();
...
The GetEmployer is shown below: GetEmployer如下所示:
public List<Employer> GetEmployer(string username, short version)
{
DateTime today = DateTime.Today;
List<Employer> employers = (ikaRepository.GetEmployers(username, today, version)).ToList<Employer>();
Debug.Assert(employers.Count() <= 1, "This is a logical Error - Can we have more than one active Employer records per user?");
return employers;
}
Here is the question: When I attached the debugger, I saw that the GetEmployer
method of the BLL is called twice. 这是一个问题:当我附加调试器时,我看到BLL的GetEmployer
方法被调用了两次。 First time on the .Select()
and second time when I try to get the value of the first field of the Employer
record. 第一次在.Select()
,第二次尝试获取Employer
记录的第一个字段的值。
Thank you 谢谢
I'm not good in English, I apologize, in advance. 我不擅长英语,我提前道歉。
You manually bound FormView
to ObjectDataSource
in code-behind. 您在代码隐藏中手动将FormView
绑定到ObjectDataSource
。 So you called Select
method once in Page
Loading phase (based on current codes). 因此,您在Page
加载阶段(基于当前代码)调用Select
方法一次。
Have you assigned "EmployerObjectDataSource"
to EmployerFormView
's DataSourceID
in your markups (ASPX contents), and bind FormView
to ObjectDataSource
in Page
Event Handling phase (after Load
phase), so it call Select
method once again. 您是否已将"EmployerObjectDataSource"
分配给标记中的EmployerFormView
的DataSourceID
(ASPX内容),并在Page
事件处理阶段( Load
阶段之后)将FormView
绑定到ObjectDataSource
,因此它再次调用Select
方法。
It is better, that you don't manually bind DataBoundControls
(like FormView
) to DataSourceControls
(like ObjectDataSource
). 最好不要手动将DataBoundControls
(如FormView
)绑定到DataSourceControls
(如ObjectDataSource
)。 Instead, you can use SelectParameters
inside ObjectDataSource
markup if it needs to pass some data to BLL for selecting/filtering logics. 相反,如果需要将一些数据传递给BLL以选择/过滤逻辑,则可以在ObjectDataSource
标记内使用SelectParameters
。
I think this situation it is what happened. 我认为这种情况就是发生了什么。 I suggest you should write your markups too. 我建议你也应该写你的标记。
Another situation is binding one ObjectDataSource
to more that one DataBoundControls
. 另一种情况是将一个ObjectDataSource
绑定到多个DataBoundControls
。 In this situation, every DataBoundControls
will call ObjectDataSource
's Select
method on it's binding-time. 在这种情况下,每个DataBoundControls
都会在它的绑定时调用ObjectDataSource
的Select
方法。 To handling this situation, you can use caching capabilities of ObjectDataSource
. 要处理这种情况,您可以使用ObjectDataSource
缓存功能。
I hope these explanations will be useful. 我希望这些解释是有用的。
I suggest you to read these resources before using ASP.NET Data Controls: 我建议您在使用ASP.NET数据控件之前阅读这些资源:
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.