📄 mydatalist.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace Example_6_4
{
/// <summary>
/// Summary description for MyDataList.
/// </summary>
public class MyDataList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList StaffList;
protected System.Web.UI.WebControls.Button CRDirection;
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///从数据库获取数据,并绑定控件的数据
BindData();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.CRDirection.Click += new System.EventHandler(this.CRDirection_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void BindData()
{
///创建访问数据库的链接
String cmdText = "SELECT Top 10 * FROM Staff ORDER BY Staff_ID";
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///打开数据库链接,并执行查询操作
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
///设置DataList控件的数据源,并绑定控件的数据
StaffList.DataSource = dr;
StaffList.DataBind();
///关闭数据库的链接
dr.Close();
myConnection.Close();
}
private void CRDirection_Click(object sender, System.EventArgs e)
{
///更改控件显示数据的方向
if(StaffList.RepeatDirection == RepeatDirection.Horizontal)
{
///垂直显示
StaffList.RepeatDirection = RepeatDirection.Vertical;
}
else
{
///水平显示
StaffList.RepeatDirection = RepeatDirection.Horizontal;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -