您的位置:首页 > 移动开发 > Objective-C

ObjectDataSourceMethodEventArgs的InputParameters 属性

2010-05-08 10:40 369 查看
如果使用 ObjectDataSourceMethodEventHandler 对象处理 Selecting、Updating、Inserting 或 Deleting 事件,则可以使用 InputParameters 属性访问和操作这些参数。此字典中参数的任何更改都将影响到为操作所调用的方法重载。当设置了 ObjectDataSource 控件的 DataObjectTypeName 属性时,只能修改此字典中各项的数据对象属性,而不能添加或移除参数

下面的代码示例演示如何使用 DropDownList 控件、TextBox 控件和多个 ObjectDataSource 控件更新数据。DropDownList 显示一个 Northwind Employee 的名称,而 TextBox 控件用于输入和更新地址信息。由于 UpdateParameters 属性包含绑定到 DropDownList 控件的选定值的 ControlParameter 对象,因此只有在选中一个雇员后才能启用触发 Update 方法的按钮。

在此示例中,在 Update 方法之前调用 NorthwindEmployeeUpdating 方法,以便将正确的参数和值添加到 InputParameters 集合。可以通过动态方式(如演示所示)或声明方式添加参数和值。

代码

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) { }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: