stocks.ascx.cs

来自「WebParts的开发的一个非常好的源码例子」· CS 代码 · 共 65 行

CS
65
字号
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Stocks_ascx:UserControl
{
	string _stocks;

	[WebBrowsable]
	[Personalizable]
	public string Stocks
	{
		get { return _stocks; }
		set
		{
			_stocks = value;
			BuildContent ();
		}
	}

	void Page_Load (object sender, EventArgs e)
	{
		if (!IsPostBack)
			BuildContent ();
	}

	void BuildContent ()
	{
		DataTable table = new DataTable("Stocks");
		table.Columns.Add(new DataColumn("Symbol", typeof(String)));
		table.Columns.Add(new DataColumn("Price", typeof(Decimal)));
		table.Columns.Add(new DataColumn("High", typeof(Decimal)));
		table.Columns.Add(new DataColumn("Low", typeof(Decimal)));

		if (String.IsNullOrEmpty(_stocks))
			_stocks = "MSFT,INTC,AMZN";

		string[] stocks = _stocks.Split(new char[] { ',', ';', ' ' });
		foreach (string stock in stocks)
			AddDataRow(table, stock, 32.5m, 24.1m, 36.2m);

		DataSet ds = new DataSet();
		ds.Tables.Add(table);

		GridView1.DataSource = ds;
		GridView1.DataBind();
	}

	void AddDataRow (DataTable table, string name, decimal price, decimal low, decimal high)
	{
		DataRow row = table.NewRow ();
		row[0] = name;
		row[1] = price;
		row[2] = low;
		row[3] = high;
		table.Rows.Add (row);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?