【问题标题】:Select method of ObjectDataSource called before update button在更新按钮之前调用的 ObjectDataSource 的 Select 方法
【发布时间】:2012-02-03 15:43:28
【问题描述】:

我有一个带有 ObjectDataSource 的数据列表,问题是当我调用更新方法时,数据列表的选择方法在更新方法之前执行,结果不显示更新的数据列表,我必须按 f5 才能看到更新的结果,下面是代码:

AspView

<asp:DataList ID="CustomersDefaultPaging" runat="server" Width="100%" 
    RepeatColumns="1" EnableViewState="False" 
    DataSourceID="CustomersDefaultPagingDataSource" DataKeyField="Id">
    <ItemTemplate>.....</ItemTemplate>
  <asp:ObjectDataSource ID="CustomersDefaultPagingDataSource" runat="server"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetCustomersAsPagedDataSource"
        TypeName="mobilecustomers" 
    OnSelected="CustomersDefaultPagingDataSource_Selected">

代码隐藏

     protected void CustomersDefaultPagingDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
    // Reference the PagedDataSource bound to the DataList
    PagedDataSource pagedData = (PagedDataSource)e.ReturnValue;

    // Remember the total number of records being paged through across postbacks
    TotalRowCount = pagedData.DataSourceCount;

    // Configure the paging interface based on the data in the PagedDataSource
    FirstPage.Enabled = !pagedData.IsFirstPage;
    PrevPage.Enabled = !pagedData.IsFirstPage;
    NextPage.Enabled = !pagedData.IsLastPage;
    LastPage.Enabled = !pagedData.IsLastPage;

    // Display the current page being viewed...
    CurrentPageNumber.Text = string.Format("You are viewing page {0} of {1}...", PageIndex + 1, PageCount);
}

     protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (DataListItem item in CustomersDefaultPaging.Items)
    {
        customers.UpdateCustomerAddress(.........);


    }
}

数据访问

      static public DataTable GetAllCustomers()
     {

    string sql = "Select * from [Customers] where [Upgrade] = 0";
    SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionString);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
    }

      public PagedDataSource GetCustomersAsPagedDataSource(string sortExpression, int pageIndex, int pageSize)
{

    DataTable dt = new DataTable();
    dt = GetAllCustomers();
    dt.DefaultView.Sort = sortExpression;
    // Limit the results through a PagedDataSource
    PagedDataSource pagedData = new PagedDataSource();
    pagedData.DataSource = dt.DefaultView;
    //pagedData.DataBind();
    pagedData.AllowPaging = true;
    pagedData.CurrentPageIndex = pageIndex;
    pagedData.PageSize = pageSize;
    return pagedData;
}

更新时单击数据列表应仅显示升级值为 0 的客户,当前当我将该字段更新为 0 时,它会更新 sql db,但它不会显示在数据列表上,我必须刷新它才能看到更新。

【问题讨论】:

    标签: asp.net objectdatasource datalist


    【解决方案1】:

    显然DataList 不会自己重新绑定。我不知道这是否可行,但是如何连接到 DataList Updated 事件并强制网格在那里绑定。这有帮助吗?

    根据我的阅读,GridView 控件与ObjectDataSource 相比效果更好。如果是我,我可能会想将DataList 换成GridView,看看是否有帮助。或者至少运行一些快速测试并查看。

    承认有点模糊

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多