下列代码实例说明了如何创建一个继承自 WebPart 类的自定义数据绑定控件,该控件能够在 Web 部件应用程序中被使用。关于如何建立这个控件以及在 ASP.NET 应用程序中使用它的详细内容,请参考:[ASP.NET 实践:建立并运行 Web 部件的数据绑定控件实例]。
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Security.Permissions; using System.Web; using System.Web.Configuration; using System.Data.Sql; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; namespace Samples.AspNet.CS.Controls { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class SmallGridWebPart : WebPart { private String connString; // Use predefined strings for commands in the data source. private const string selectStmt = @"Select * from [Customers]"; private const string deleteCmd = @"DELETE FROM [Customers] " + @"WHERE [CustomerID] = @CustomerID"; private const string insertCmd = @"INSERT INTO [Customers] " + @"([CustomerID], [CompanyName], [ContactName], " + @"[Phone]) VALUES (@CustomerID, @CompanyName, " + @"@ContactName, @Phone)"; private const string updateCmd = @"UPDATE [Customers] SET " + @"[CompanyName] = @CompanyName, [ContactName] = @ContactName, " + @"[Phone] = @Phone WHERE [CustomerID] = @CustomerID"; public SmallGridWebPart() { ExportMode = WebPartExportMode.All; } // 这个重载防止用户关闭控件。 public override bool AllowClose { get { return false; } // 没有实现 set 访问器。我们保留它是因为基类中包含这个属性,但是我们需要防止用户关闭控件以及开发者对该属性的更新。 set { ; } } // 允许页面开发者设置连接串。 public String ConnectionString { get { return connString; } set { connString = value; } } protected override void CreateChildControls() { Controls.Clear(); // 创建 SqlDataSource 控件。 SqlDataSource ds = new SqlDataSource(this.ConnectionString, selectStmt); ds.ID = "dsCustomers"; ds.DataSourceMode = SqlDataSourceMode.DataSet; ds.InsertCommandType = SqlDataSourceCommandType.Text; ds.InsertCommand = insertCmd; ds.UpdateCommandType = SqlDataSourceCommandType.Text; ds.UpdateCommand = updateCmd; ds.DeleteCommandType = SqlDataSourceCommandType.Text; ds.DeleteCommand = deleteCmd; ParameterCollection deleteParams = new ParameterCollection(); deleteParams.Add(BuildParam("CustomerID", TypeCode.String)); ParameterCollection insertParams = new ParameterCollection(); insertParams.Add(BuildParam("CustomerID", TypeCode.String)); insertParams.Add(BuildParam("CompanyName", TypeCode.String)); insertParams.Add(BuildParam("ContactName", TypeCode.String)); insertParams.Add(BuildParam("Phone", TypeCode.String)); ParameterCollection updateParams = new ParameterCollection(); updateParams.Add(BuildParam("CustomerID", TypeCode.String)); updateParams.Add(BuildParam("CompanyName", TypeCode.String)); updateParams.Add(BuildParam("ContactName", TypeCode.String)); updateParams.Add(BuildParam("Phone", TypeCode.String)); this.Controls.Add(ds); // 创建 GridView 控件并把它绑定到 SqlDataSource。 GridView grid = new GridView(); grid.ID = "customerGrid"; grid.Font.Size = 8; grid.AllowPaging = true; grid.AllowSorting = true; grid.AutoGenerateColumns = false; String[] fields = { "CustomerID" }; grid.DataKeyNames = fields; grid.DataSourceID = "dsCustomers"; CommandField controlButton = new CommandField(); controlButton.ShowEditButton = true; controlButton.ShowSelectButton = true; grid.Columns.Add(controlButton); BoundField customerID = BuildBoundField("CustomerID"); customerID.ReadOnly = true; grid.Columns.Add(customerID); grid.Columns.Add(BuildBoundField("CompanyName")); grid.Columns.Add(BuildBoundField("ContactName")); grid.Columns.Add(BuildBoundField("Phone")); this.Controls.Add(grid); } private Parameter BuildParam(String paramName, TypeCode dataTypeCode) { Parameter theParm = new Parameter(paramName, dataTypeCode); return theParm; } private BoundField BuildBoundField(String fieldName) { BoundField theField = new BoundField(); theField.DataField = fieldName; theField.HeaderText = fieldName; theField.SortExpression = fieldName; return theField; } } }