|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
ObjectDataSourceMethodEventArgs-Klasse
System.EventArgs
System.ComponentModel.CancelEventArgs
System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs
System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Der ObjectDataSourceMethodEventArgs-Typ macht die folgenden Member verfügbar.
| Name | Beschreibung | |
|---|---|---|
![]() | ObjectDataSourceMethodEventArgs |
| Name | Beschreibung | |
|---|---|---|
![]() | Cancel | |
![]() | InputParameters |
| Name | Beschreibung | |
|---|---|---|
![]() | Equals(Object) | |
![]() | Finalize | |
![]() | GetHashCode | |
![]() | GetType | |
![]() | MemberwiseClone | |
![]() | ToString |
ObjectDataSourceMethodEventArgs | ||
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %> <%@ Import namespace="Samples.AspNet.CS" %> <%@ Page language="c#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> private void NorthwindEmployeeInserting(object source, ObjectDataSourceMethodEventArgs e) { // The business object expects a custom type. Build it // and add it to the parameters collection. IDictionary paramsFromPage = e.InputParameters; NorthwindEmployee ne = new NorthwindEmployee(); ne.FirstName = paramsFromPage["FirstName"].ToString(); ne.LastName = paramsFromPage["LastName"].ToString(); ne.Title = paramsFromPage["Title"].ToString(); ne.Courtesy = paramsFromPage["Courtesy"].ToString(); ne.Supervisor = Int32.Parse(paramsFromPage["Supervisor"].ToString()); paramsFromPage.Clear(); paramsFromPage.Add("ne", ne); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>ObjectDataSource - C# Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:detailsview id="DetailsView1" runat="server" autogenerateinsertbutton="True" datasourceid="ObjectDataSource1"> </asp:detailsview> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetEmployee" insertmethod="UpdateEmployeeInfo" oninserting="NorthwindEmployeeInserting" typename="Samples.AspNet.CS.EmployeeLogic" > <selectparameters> <asp:parameter name="anID" defaultvalue="-1" /> </selectparameters> </asp:objectdatasource> </form> </body> </html>
Die EmployeeLogic-Klasse, die eine zustandslose Klasse ist, die Geschäftslogik kapselt. Die NorthwindEmployee-Klasse, die eine Modellklasse ist, die nur die grundlegenden Funktionen enthält, die erforderlich ist, um Daten aus der Datenebene zu laden und beizubehalten.
namespace Samples.AspNet.CS { using System; using System.Collections; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; // // EmployeeLogic is a stateless business object that encapsulates // the operations you can perform on a NorthwindEmployee object. // public class EmployeeLogic { // Returns a collection of NorthwindEmployee objects. public static ICollection GetAllEmployees () { ArrayList al = new ArrayList(); ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"]; SqlDataSource sds = new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees"); try { IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty); // Iterate through the Enumeration and create a // NorthwindEmployee object for each ID. IEnumerator enumerator = IDs.GetEnumerator(); while (enumerator.MoveNext()) { // The IEnumerable contains DataRowView objects. DataRowView row = enumerator.Current as DataRowView; string id = row["EmployeeID"].ToString(); NorthwindEmployee nwe = new NorthwindEmployee(id); // Add the NorthwindEmployee object to the collection. al.Add(nwe); } } finally { // If anything strange happens, clean up. sds.Dispose(); } return al; } public static NorthwindEmployee GetEmployee(object anID) { if (anID.Equals("-1") || anID.Equals(DBNull.Value) ) { return new NorthwindEmployee(); } else { return new NorthwindEmployee(anID); } } public static void UpdateEmployeeInfo(NorthwindEmployee ne) { bool retval = ne.Save(); if (! retval) { throw new NorthwindDataException("UpdateEmployee failed."); } } public static void DeleteEmployee(NorthwindEmployee ne) { bool retval = ne.Delete(); if (! retval) { throw new NorthwindDataException("DeleteEmployee failed."); } } // And so on... } public class NorthwindEmployee { public NorthwindEmployee () { ID = DBNull.Value; lastName = ""; firstName = ""; title=""; titleOfCourtesy = ""; reportsTo = -1; } public NorthwindEmployee (object anID) { this.ID = anID; SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings["NorthwindConnection"].ConnectionString); SqlCommand sc = new SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " + " FROM Employees " + " WHERE EmployeeID = @empId", conn); // Add the employee ID parameter and set its value. sc.Parameters.Add(new SqlParameter("@empId",SqlDbType.Int)).Value = Int32.Parse(anID.ToString()); SqlDataReader sdr = null; try { conn.Open(); sdr = sc.ExecuteReader(); // Only loop once. if (sdr != null && sdr.Read()) { // The IEnumerable contains DataRowView objects. this.firstName = sdr["FirstName"].ToString(); this.lastName = sdr["LastName"].ToString(); this.title = sdr["Title"].ToString(); this.titleOfCourtesy = sdr["TitleOfCourtesy"].ToString(); if (! sdr.IsDBNull(4)) { this.reportsTo = sdr.GetInt32(4); } } else { throw new NorthwindDataException("Data not loaded for employee id."); } } finally { try { if (sdr != null) sdr.Close(); conn.Close(); } catch (SqlException) { // Log an event in the Application Event Log. throw; } } } private object ID; public string EmpID { get { return ID.ToString(); } } private string lastName; public string LastName { get { return lastName; } set { lastName = value; } } private string firstName; public string FirstName { get { return firstName; } set { firstName = value; } } public string FullName { get { return FirstName + " " + LastName; } } private string title; public String Title { get { return title; } set { title = value; } } private string titleOfCourtesy; public string Courtesy { get { return titleOfCourtesy; } set { titleOfCourtesy = value; } } private int reportsTo; public int Supervisor { get { return reportsTo; } set { reportsTo = value; } } public bool Save () { // Implement persistence logic. return true; } public bool Delete () { // Implement delete logic. return true; } } internal class NorthwindDataException: Exception { public NorthwindDataException(string msg) : base (msg) { } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core-Rolle wird nicht unterstützt), Windows Server 2008 R2 (Server Core-Rolle wird mit SP1 oder höher unterstützt; Itanium wird nicht unterstützt)
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
