ObjectDataSource.Updating 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在 Update() 作業之前發生。
public:
event System::Web::UI::WebControls::ObjectDataSourceMethodEventHandler ^ Updating;
public event System.Web.UI.WebControls.ObjectDataSourceMethodEventHandler Updating;
member this.Updating : System.Web.UI.WebControls.ObjectDataSourceMethodEventHandler
Public Custom Event Updating As ObjectDataSourceMethodEventHandler
事件類型
範例
下列三個範例顯示網頁、程序代碼後置頁面類別,以及數據存取類別,可讓使用者擷取和更新 Northwind 資料庫中 Employees 數據表中的記錄。
第一個範例顯示包含兩 ObjectDataSource 個控件、一個控件和一個 DropDownList 控件的 DetailsView 網頁。 第一個 ObjectDataSource 控件和 DropDownList 控件是用來從資料庫擷取和顯示員工名稱。 第二 ObjectDataSource 個 DetailsView 控件和控件是用來擷取、顯示和修改使用者所選取員工記錄中的數據。
<form id="Form1" method="post" runat="server">
<asp:objectdatasource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetFullNamesAndIDs"
TypeName="Samples.AspNet.CS.EmployeeLogic" />
<p>
<asp:dropdownlist
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="FullName"
DataValueField="EmployeeID"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
</asp:dropdownlist>
</p>
<asp:objectdatasource
ID="ObjectDataSource2"
runat="server"
SelectMethod="GetEmployee"
UpdateMethod="UpdateEmployeeAddress"
OnUpdating="EmployeeUpdating"
OnSelected="EmployeeSelected"
TypeName="Samples.AspNet.CS.EmployeeLogic" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
</SelectParameters>
</asp:objectdatasource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
DataSourceID="ObjectDataSource2"
AutoGenerateRows="false"
AutoGenerateEditButton="true">
<Fields>
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
</Fields>
</asp:DetailsView>
</form>
<form id="form1" runat="server">
<asp:objectdatasource
ID="ObjectDataSource1"
runat="server"
SelectMethod="GetFullNamesAndIDs"
TypeName="Samples.AspNet.CS.EmployeeLogic" />
<p>
<asp:dropdownlist
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="FullName"
DataValueField="EmployeeID"
AutoPostBack="True"
AppendDataBoundItems="true">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
</asp:dropdownlist>
</p>
<asp:objectdatasource
ID="ObjectDataSource2"
runat="server"
SelectMethod="GetEmployee"
UpdateMethod="UpdateEmployeeAddress"
OnUpdating="EmployeeUpdating"
OnSelected="EmployeeSelected"
TypeName="Samples.AspNet.CS.EmployeeLogic" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
</SelectParameters>
</asp:objectdatasource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
DataSourceID="ObjectDataSource2"
AutoGenerateRows="false"
AutoGenerateEditButton="true">
<Fields>
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
</Fields>
</asp:DetailsView>
</form>
第二個範例顯示 和 Updating 事件的處理程式Selected。 事件處理程式會 Selected 串行化物件,其中包含從 Employee 數據表擷取的數據。 串行化物件會儲存在檢視狀態。 事件處理程式會 Updating 以檢視狀態還原串行化物件,其中包含正在更新之數據記錄的原始數據。 包含原始數據的 物件會當做參數傳遞至 Update 方法。 原始數據必須傳遞至資料庫,以便用來檢查資料是否已由另一個進程修改。
public void EmployeeUpdating(object source, ObjectDataSourceMethodEventArgs e)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
String xmlData = ViewState["OriginalEmployee"].ToString();
XmlReader reader = XmlReader.Create(new StringReader(xmlData));
Employee originalEmployee = (Employee)dcs.ReadObject(reader);
reader.Close();
e.InputParameters.Add("originalEmployee", originalEmployee);
}
public void EmployeeSelected(object source, ObjectDataSourceStatusEventArgs e)
{
if (e.ReturnValue != null)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
dcs.WriteObject(writer, e.ReturnValue);
writer.Close();
ViewState["OriginalEmployee"] = sb.ToString();
}
}
Public Sub EmployeeUpdating(ByVal source As Object, ByVal e As ObjectDataSourceMethodEventArgs)
Dim dcs As New DataContractSerializer(GetType(Employee))
Dim xmlData As String
Dim reader As XmlReader
Dim originalEmployee As Employee
xmlData = ViewState("OriginalEmployee").ToString()
reader = XmlReader.Create(New StringReader(xmlData))
originalEmployee = CType(dcs.ReadObject(reader), Employee)
reader.Close()
e.InputParameters.Add("originalEmployee", originalEmployee)
End Sub
Public Sub EmployeeSelected(ByVal source As Object, ByVal e As ObjectDataSourceStatusEventArgs)
If e.ReturnValue IsNot Nothing Then
Dim dcs As New DataContractSerializer(GetType(Employee))
Dim sb As New StringBuilder()
Dim writer As XmlWriter
writer = XmlWriter.Create(sb)
dcs.WriteObject(writer, e.ReturnValue)
writer.Close()
ViewState("OriginalEmployee") = sb.ToString()
End If
End Sub
第三個範例顯示與 Northwind 資料庫互動的數據存取類別。 類別會使用 LINQ 來查詢及更新 Employees 數據表。 此範例需要代表 Northwind 資料庫和 Employees 數據表的 LINQ to SQL 類別。 如需詳細資訊,請參閱 如何:在 Web 專案中建立 LINQ to SQL 類別。
public class EmployeeLogic
{
public static Array GetFullNamesAndIDs()
{
NorthwindDataContext ndc = new NorthwindDataContext();
var employeeQuery =
from e in ndc.Employees
orderby e.LastName
select new { FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID };
return employeeQuery.ToArray();
}
public static Employee GetEmployee(int empID)
{
if (empID < 0)
{
return null;
}
else
{
NorthwindDataContext ndc = new NorthwindDataContext();
var employeeQuery =
from e in ndc.Employees
where e.EmployeeID == empID
select e;
return employeeQuery.Single();
}
}
public static void UpdateEmployeeAddress(Employee originalEmployee, string address, string city, string postalcode)
{
NorthwindDataContext ndc = new NorthwindDataContext();
ndc.Employees.Attach(originalEmployee, false);
originalEmployee.Address = address;
originalEmployee.City = city;
originalEmployee.PostalCode = postalcode;
ndc.SubmitChanges();
}
}
Public Class EmployeeLogic
Public Shared Function GetFullNamesAndIDs() As Array
Dim ndc As New NorthwindDataContext()
Dim employeeQuery = _
From e In ndc.Employees _
Order By e.LastName _
Select FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID
Return employeeQuery.ToArray()
End Function
Public Shared Function GetEmployee(ByVal empID As Integer) As Employee
If (empID < 0) Then
Return Nothing
Else
Dim ndc As New NorthwindDataContext()
Dim employeeQuery = _
From e In ndc.Employees _
Where e.EmployeeID = empID _
Select e
Return employeeQuery.Single()
End If
End Function
Public Shared Sub UpdateEmployeeAddress(ByVal originalEmployee As Employee, ByVal address As String, ByVal city As String, ByVal postalcode As String)
Dim ndc As New NorthwindDataContext()
ndc.Employees.Attach(originalEmployee, False)
originalEmployee.Address = address
originalEmployee.City = city
originalEmployee.PostalCode = postalcode
ndc.SubmitChanges()
End Sub
End Class
備註
Updating處理 事件以執行應用程式特有的其他初始化、驗證參數的值,或變更控件執行更新作業之前ObjectDataSource的參數值。 參數可作為 IDictionary 屬性所存取的 InputParameters 集合,由 對象公開 ObjectDataSourceMethodEventArgs 。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。