RE: ObjectDataSource in ASP.NET 2.0
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Tue, 01 Nov 2005 08:24:08 GMT
Hi Ole M,
Welcome to ASPNET newsgroup.
Regarding on the ObjectDataSource's update method's question, I think it's
the normal behavior which is expected.
For datasourcecontrol and databindig control, after the object data source
pass the data bojects to data controls, it no longer hold the actual
refernce of the oringial data(nor does the data binding control), also
since the actual backend datasource may vary (maybe database or xml file or
....), it don't quite makesense to maintain such a in-memory reference. In
fact, this also somewhat related to the ASP.NET application's runtime
model, it's request/response based, generally after page output response to
client, all the serverside objects will be disposed and in-memory reference
will no longer make sense in the next page lifecycle. This is diferent
from winform application. So in such scenario, we should update the
original object through its primary key value or retrieve the reference
again from orginal datasource, e.g:
======================
private static List<Employee> _list;
.......
public static void UpdateEmployee(Employee emp)
{
EmployeePredicate ep = new EmployeePredicate(emp.ID);
Employee employee = _list.Find(ep.Assert);
if (employee == null)
{
throw new Exception("Invalid Employee info in
UpdateEmployee()...");
}
employee.Name = emp.Name;
employee.Email = emp.Email;
}
public class EmployeePredicate
{
private long _id;
public EmployeePredicate(long id)
{
_id = id;
}
public bool Assert(Employee emp)
{
if (emp.ID == _id)
{
return true;
}
else
{
return false;
}
}
}
============================
Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Ole M" <olem@xxxxxxxxxxxxxxxx>
| Subject: ObjectDataSource in ASP.NET 2.0
| Date: Mon, 31 Oct 2005 22:03:59 +0100
| Lines: 113
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#aojf6l3FHA.3628@xxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135092
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.
|
| I have a wrapper that contains the static methods for Select and Update.
The
| Update-method takes the business object as parameter.
|
| When the Update-method is invoked by the ObjectDataSource, the object
| referenced is not the same object returned by the Select-method, but a
new
| object with only the values from the Edit-template. So basically I get a
| reference to a useless object here because it cannot be used by the DAL.
|
| Is this a bug or a feature?
|
| Here is some code to illustrate:
|
| <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
| DataSourceID="ObjectDataSource1">
|
| <Columns>
|
| <asp:CommandField ShowEditButton="True" />
|
| <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
/>
|
| </Columns>
|
| </asp:GridView>
|
| <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
| DataObjectTypeName="Customer"
|
| SelectMethod="GetCustomers" TypeName="MyWrapperObj"
| UpdateMethod="Save"></asp:ObjectDataSource>
|
| And the C#-code:
|
| public class Customer
|
| {
|
| private int id;
|
| public int Id
|
| {
|
| get { return id; }
|
| set { id = value; }
|
| }
|
| private string name;
|
| public string Name
|
| {
|
| get { return name; }
|
| set { name = value; }
|
| }
|
| public void Save()
|
| {
|
| // Do some things here...
|
| }
|
| }
|
| public class MyWrapperObj
|
| {
|
| public static IList<Customer> GetCustomers()
|
| {
|
| List<Customer> customers = new List<Customer>();
|
| for (int i = 0; i < 10; i++)
|
| {
|
| Customer customer = new Customer();
|
| customer.Id = i;
|
| customer.Name = string.Format("Name {0}", i);
|
|
| customers.Add(customer);
|
| }
|
| return customers;
|
| }
|
| public static void Save(Customer obj)
|
| {
|
| obj.Save(); // This object is NOT in the IList returned by GetCustomers()
|
| }
|
| }
|
|
|
.
- Prev by Date: RE: Error: CrystalDecisions.CrystalReports
- Next by Date: Re: How to make first combo box entry blank?
- Previous by thread: RE: Error: CrystalDecisions.CrystalReports
- Next by thread: Re: ObjectDataSource in ASP.NET 2.0
- Index(es):