gridwebpart .cs
来自「asp。net 2.0宝典一书源码 全书源码给大家共享」· CS 代码 · 共 88 行
CS
88 行
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 WebPartDemo
{
[AspNetHostingPermission(SecurityAction.Demand,Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,Level = AspNetHostingPermissionLevel.Minimal)]
public class GridWebPart : WebPart
{
private String connString;
// Use predefined strings for commands in the data source.
private const string selectStmt = @"Select * from [Customers]";
public GridWebPart()
{
ExportMode = WebPartExportMode.All;
}
// This override prevents users from closing the control.
public override bool AllowClose
{
get
{
return false;
}
set { ; }
}
public String ConnectionString
{
get { return connString; }
set { connString = value; }
}
protected override void CreateChildControls()
{
Controls.Clear();
SqlDataSource ds = new SqlDataSource(this.ConnectionString, selectStmt);
ds.ID = "dsCustomers";
this.Controls.Add(ds);
GridView grid = new GridView();
grid.ID = "customerGrid";
grid.Font.Size = 8;
grid.AllowPaging = true;
grid.AllowSorting = false;
grid.AutoGenerateColumns = false;
String[] fields = { "CustomerID" };
grid.DataKeyNames = fields;
grid.DataSourceID = "dsCustomers";
CommandField controlButton = new CommandField();
controlButton.ShowEditButton = false;
controlButton.ShowSelectButton = false;
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 BoundField BuildBoundField(String fieldName)
{
BoundField theField = new BoundField();
theField.DataField = fieldName;
theField.HeaderText = fieldName;
theField.SortExpression = fieldName;
return theField;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?