更新按钮之前调用的 ObjectDataSource 的 Select 方法
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然
DataList
并不进行自己的重新绑定。我不知道这是否可行,但是如何挂钩 DataListUpdated
事件并强制网格绑定在那里。这有帮助吗?根据我的阅读,
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 DataListUpdated
event and forcing the grid to Bind there. Could that help?From what I've read the
GridView
control plays a lot nicer with theObjectDataSource
. It it were me I might be tempted to swap out theDataList
for aGridView
and see if that helps. Or at least run some quick tests and see.All a bit vague admittedly