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:
- I have an ObjectDataSource (called
EmployerObjectDataSource) that is using a method of business logic layer (BLL) object for selecting the data - the method isGetEmployer - In the
Page_PreRendercallback of my page, I call a methodpopulateFieldsto populate the fields within a FormView - In the
populateFieldsI callEmployerObjectDataSource.Select()to get theEmployerrecord. - If there are any records returned, then I populate the text boxes with the values from the returned 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