ObjectDataSourceView.OnUpdating Method
Assembly: System.Web (in system.web.dll)
protected void OnUpdating ( ObjectDataSourceMethodEventArgs e )
protected function OnUpdating ( e : ObjectDataSourceMethodEventArgs )
Not applicable.
Parameters
- e
An ObjectDataSourceMethodEventArgs that contains the event data.
Raising an event invokes the event handler through a delegate. For more information, see Consuming Events.
The OnUpdating method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
Notes to Inheritors: When overriding the OnUpdating method in a derived class, be sure to call the OnUpdating method for the base class so that registered delegates receive the event.The following code example demonstrates how to use a DropDownList control, TextBox controls, and several ObjectDataSource controls to update data. The DropDownList displays the name of a NorthwindEmployee, while the TextBox controls are used to enter and update address information. Because the UpdateParameters collection contains a ControlParameter object that is bound to the selected value of the DropDownList, the button that triggers the Update operation is enabled only after an employee is selected.
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %>
<%@ Page Language="VJ#" %>
<%@ Import namespace="Samples.AspNet.JSL" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// Add parameters and initialize the user interface
// only if an employee is selected.
private void Page_Load(Object sender, System.EventArgs e) throws NorthwindDataException
{
// Be sure the text boxes are initialized with
// data from the currently selected employee.
NorthwindEmployee selectedEmployee =
EmployeeLogic.GetEmployee(DropDownList1.get_SelectedValue());
if (selectedEmployee != null) {
AddressBox.set_Text(selectedEmployee.get_Address());
CityBox.set_Text(selectedEmployee.get_City());
PostalCodeBox.set_Text(selectedEmployee.get_PostalCode());
Button1.set_Enabled(true);
}
else {
Button1.set_Enabled(false);
}
} //Page_Load
// Press the button to update.
private void Btn_UpdateEmployee(Object sender, CommandEventArgs e)
{
ObjectDataSource2.Update();
} //Btn_UpdateEmployee
// Dynamically add parameters to the InputParameters collection.
private void NorthwindEmployeeUpdating(Object source,
ObjectDataSourceMethodEventArgs e)
{
// The names of the parameters are the same as
// the variable names for the method that is invoked to
// perform the Update. The InputParameters collection is
// an IDictionary collection of name/value pairs,
// not a ParameterCollection.
e.get_InputParameters().Add("anID", DropDownList1.get_SelectedValue());
e.get_InputParameters().Add("anAddress", AddressBox.get_Text());
e.get_InputParameters().Add("aCity", CityBox.get_Text());
e.get_InputParameters().Add("aPostalCode", PostalCodeBox.get_Text());
} //NorthwindEmployeeUpdating
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - VJ# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<!-- The DropDownList is bound to the first ObjectDataSource. -->
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployees"
typename="Samples.AspNet.JSL.EmployeeLogic" />
<p><asp:dropdownlist
id="DropDownList1"
runat="server"
datasourceid="ObjectDataSource1"
datatextfield="FullName"
datavaluefield="EmpID"
autopostback="True" /></p>
<!-- The second ObjectDataSource performs the Update. This
preserves the state of the DropDownList, which otherwise
would rebind when the DataSourceChanged event is
raised as a result of an Update operation. -->
<asp:objectdatasource
id="ObjectDataSource2"
runat="server"
updatemethod="UpdateEmployeeWrapper"
onupdating="NorthwindEmployeeUpdating"
typename="Samples.AspNet.JSL.EmployeeLogic" />
<p><asp:textbox
id="AddressBox"
runat="server" /></p>
<p><asp:textbox
id="CityBox"
runat="server" /></p>
<p><asp:textbox
id="PostalCodeBox"
runat="server" /></p>
<asp:button
id="Button1"
runat="server"
text="Update Employee"
oncommand="Btn_UpdateEmployee" />
</form>
</body>
</html>