更新按钮之前调用的 ObjectDataSource 的 Select 方法

发布于 2025-01-02 10:01:32 字数 2646 浏览 2 评论 0原文

我有一个带有 ObjectDataSource 的数据列表,问题是当我调用更新方法时,数据列表的 select 方法在更新方法之前执行,结果不显示更新的数据列表,我必须按 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">

CodeBehind

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


    }
}

DataAcess

      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 数据库,但不会显示在datalist 我必须刷新它才能看到更新。

I have got a datalist with ObjectDataSource , The issue is when i call the update method , the selectmethod of datalist is executed before the update method, which in result doesnt shows the updated datalist , i have to f5 to see the updated results, below is the code:

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">

CodeBehind

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


    }
}

DataAcess

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

On update click the datalist should display only customers whose upgrade value is 0, Curretly when i update the field to 0 it update the sql db but it doesnt shows on the datalist i have to refresh it in order to see the updates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2025-01-09 10:01:33

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

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

诚然,一切都有点模糊

Apparently the DataList doesn't do it's own rebinding. I don't know if this would work but what about hooking into the DataList Updated event and forcing the grid to Bind there. Could that help?

From what I've read the GridView control plays a lot nicer with the ObjectDataSource. It it were me I might be tempted to swap out the DataList for a GridView and see if that helps. Or at least run some quick tests and see.

All a bit vague admittedly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文