ObjectDataSource.Selected Event
ObjectDataSource.Selected Event
Occurs when a Select operation has completed.
Occurs when a Select operation has completed.
Code
1<form id="Form1" method="post" runat="server">
2
3 <asp:objectdatasource
4 ID="ObjectDataSource1"
5 runat="server"
6 SelectMethod="GetFullNamesAndIDs"
7 TypeName="Samples.AspNet.CS.EmployeeLogic" />
8
9 <p>
10 <asp:dropdownlist
11 ID="DropDownList1"
12 runat="server"
13 DataSourceID="ObjectDataSource1"
14 DataTextField="FullName"
15 DataValueField="EmployeeID"
16 AutoPostBack="True"
17 AppendDataBoundItems="true">
18 <asp:ListItem Text="Select One" Value=""></asp:ListItem>
19 </asp:dropdownlist>
20 </p>
21
22 <asp:objectdatasource
23 ID="ObjectDataSource2"
24 runat="server"
25 SelectMethod="GetEmployee"
26 UpdateMethod="UpdateEmployeeAddress"
27 OnUpdating="EmployeeUpdating"
28 OnSelected="EmployeeSelected"
29 TypeName="Samples.AspNet.CS.EmployeeLogic" >
30 <SelectParameters>
31 <asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
32 </SelectParameters>
33 </asp:objectdatasource>
34
35 <asp:DetailsView
36 ID="DetailsView1"
37 runat="server"
38 DataSourceID="ObjectDataSource2"
39 AutoGenerateRows="false"
40 AutoGenerateEditButton="true">
41 <Fields>
42 <asp:BoundField HeaderText="Address" DataField="Address" />
43 <asp:BoundField HeaderText="City" DataField="City" />
44 <asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
45 </Fields>
46 </asp:DetailsView>
47
48</form>
1<form id="Form1" method="post" runat="server">
2
3 <asp:objectdatasource
4 ID="ObjectDataSource1"
5 runat="server"
6 SelectMethod="GetFullNamesAndIDs"
7 TypeName="Samples.AspNet.CS.EmployeeLogic" />
8
9 <p>
10 <asp:dropdownlist
11 ID="DropDownList1"
12 runat="server"
13 DataSourceID="ObjectDataSource1"
14 DataTextField="FullName"
15 DataValueField="EmployeeID"
16 AutoPostBack="True"
17 AppendDataBoundItems="true">
18 <asp:ListItem Text="Select One" Value=""></asp:ListItem>
19 </asp:dropdownlist>
20 </p>
21
22 <asp:objectdatasource
23 ID="ObjectDataSource2"
24 runat="server"
25 SelectMethod="GetEmployee"
26 UpdateMethod="UpdateEmployeeAddress"
27 OnUpdating="EmployeeUpdating"
28 OnSelected="EmployeeSelected"
29 TypeName="Samples.AspNet.CS.EmployeeLogic" >
30 <SelectParameters>
31 <asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
32 </SelectParameters>
33 </asp:objectdatasource>
34
35 <asp:DetailsView
36 ID="DetailsView1"
37 runat="server"
38 DataSourceID="ObjectDataSource2"
39 AutoGenerateRows="false"
40 AutoGenerateEditButton="true">
41 <Fields>
42 <asp:BoundField HeaderText="Address" DataField="Address" />
43 <asp:BoundField HeaderText="City" DataField="City" />
44 <asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
45 </Fields>
46 </asp:DetailsView>
47
48</form>
Code
1public void EmployeeUpdating(object source, ObjectDataSourceMethodEventArgs e)
2{
3 DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
4
5 String xmlData = ViewState["OriginalEmployee"].ToString();
6 XmlReader reader = XmlReader.Create(new StringReader(xmlData));
7 Employee originalEmployee = (Employee)dcs.ReadObject(reader);
8 reader.Close();
9
10 e.InputParameters.Add("originalEmployee", originalEmployee);
11}
12
13public void EmployeeSelected(object source, ObjectDataSourceStatusEventArgs e)
14{
15 if (e.ReturnValue != null)
16 {
17 DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
18 StringBuilder sb = new StringBuilder();
19 XmlWriter writer = XmlWriter.Create(sb);
20 dcs.WriteObject(writer, e.ReturnValue);
21 writer.Close();
22
23 ViewState["OriginalEmployee"] = sb.ToString();
24 }
25}
1public void EmployeeUpdating(object source, ObjectDataSourceMethodEventArgs e)
2{
3 DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
4
5 String xmlData = ViewState["OriginalEmployee"].ToString();
6 XmlReader reader = XmlReader.Create(new StringReader(xmlData));
7 Employee originalEmployee = (Employee)dcs.ReadObject(reader);
8 reader.Close();
9
10 e.InputParameters.Add("originalEmployee", originalEmployee);
11}
12
13public void EmployeeSelected(object source, ObjectDataSourceStatusEventArgs e)
14{
15 if (e.ReturnValue != null)
16 {
17 DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
18 StringBuilder sb = new StringBuilder();
19 XmlWriter writer = XmlWriter.Create(sb);
20 dcs.WriteObject(writer, e.ReturnValue);
21 writer.Close();
22
23 ViewState["OriginalEmployee"] = sb.ToString();
24 }
25}