【问题标题】:SelectMethod called twice for ObjectDataSource为 ObjectDataSource 调用了两次 SelectMethod
【发布时间】:2013-11-19 21:22:54
【问题描述】:

由于我是 ASP.NET 网络表单和实体框架的新手,我正在尝试一个宠物项目。

在此期间,我遇到了以下我想了解的内容:

  1. 我有一个 ObjectDataSource(称为 EmployerObjectDataSource),它使用业务逻辑层 (BLL) 对象的方法来选择数据 - 该方法是 GetEmployer
  2. 在我的页面的Page_PreRender 回调中,我调用了一个方法populateFields 来填充FormView 中的字段
  3. populateFields 中,我调用EmployerObjectDataSource.Select() 以获取Employer 记录。
  4. 如果返回了任何记录,则使用返回记录中的值填充文本框。

代码如下:

    //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();
  ...

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;
    }

这是一个问题: 当我附加调试器时,我看到 BLL 的GetEmployer 方法被调用了两次。第一次在.Select() 上,第二次尝试获取Employer 记录的第一个字段的值。

谢谢

【问题讨论】:

  • 为什么要在 PreRender 中而不是在 Load 中绑定数据?
  • @DmytroRudenko 我试图解决另一个问题,然后就这样离开了。没错,Page_Load 更合适,但这对我发布的问题没有影响。
  • 也许你在employer.AME 财产的getter 中有一些小窍门?我找不到任何其他可能会调用 Select() 的原因。

标签: c# asp.net webforms repository-pattern objectdatasource


【解决方案1】:

我的英语不好,提前道歉。

您在代码隐藏中手动将FormView 绑定到ObjectDataSource。所以你在Page Loading 阶段调用了一次Select 方法(基于当前代码)。

您是否在标记(ASPX 内容)中将"EmployerObjectDataSource" 分配给EmployerFormViewDataSourceID,并在Page 事件处理 阶段将FormView 绑定到ObjectDataSource(在Load 阶段之后),所以它再次调用Select 方法。

最好不要手动将DataBoundControls(如FormView)绑定到DataSourceControls(如ObjectDataSource)。相反,如果需要将一些数据传递给 BLL 以进行选择/过滤逻辑,您可以在 ObjectDataSource 标记中使用 SelectParameters

我认为这种情况就是这样发生的。我建议你也应该编写标记。

另一种情况是将一个ObjectDataSource 绑定到多个DataBoundControls。 在这种情况下,每个DataBoundControls 都会在其绑定时间调用ObjectDataSourceSelect 方法。要处理这种情况,您可以使用ObjectDataSource 的缓存功能。

我希望这些解释会有用。

我建议您在使用 ASP.NET 数据控件之前阅读这些资源:

  1. ObjectDataSource on MSDN
  2. FormView on MSDN
  3. ASP.NET Page Life Cycle Overview on MSDN

祝你好运!

【讨论】:

  • (+1) 您可能还提到FormView 只需要记录或行,而不是许多记录,因此绑定到多个ObjectDataSource 控件可能会导致一些冲突。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 2014-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多