as I am new to ASP.NET webforms and Entity framework, I am experimenting with a pet project.
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
Page_PreRender
callback of my page, I call a method populateFields
to populate the fields within a FormViewpopulateFields
I call EmployerObjectDataSource.Select()
to get the Employer
record. 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:
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. First time on the .Select()
and second time when I try to get the value of the first field of the Employer
record.
Thank you
I'm not good in English, I apologize, in advance.
You manually bound FormView
to ObjectDataSource
in code-behind. So you called Select
method once in Page
Loading phase (based on current codes).
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.
It is better, that you don't manually bind DataBoundControls
(like FormView
) to DataSourceControls
(like ObjectDataSource
). Instead, you can use SelectParameters
inside ObjectDataSource
markup if it needs to pass some data to BLL for selecting/filtering logics.
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
.
In this situation, every DataBoundControls
will call ObjectDataSource
's Select
method on it's binding-time. To handling this situation, you can use caching capabilities of ObjectDataSource
.
I hope these explanations will be useful.
I suggest you to read these resources before using ASP.NET Data Controls:
Good luck!