📄 databindingeg.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;
namespace aspnet.chapter11
{
/// <summary>
/// DataBinding 的摘要说明。
/// </summary>
public class DataBindingEg : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DpDnLst;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Panel panel2;
protected System.Web.UI.WebControls.DataGrid db3;
protected System.Web.UI.WebControls.Panel panel1;
protected System.Web.UI.WebControls.Repeater db1;
protected System.Web.UI.WebControls.DataList db2;
protected System.Web.UI.WebControls.Panel panel3;
private void Page_Load(object sender, System.EventArgs e)
{
DataSet myds;
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
try
{
//指定连接的服务器、用户、口令、数据库
System.Data.SqlClient.SqlConnection sqlConn=new System.Data.SqlClient.SqlConnection("server=LIUFPAD;uid=sa;pwd=;database=pubs");
//System.Data.SqlClient.SqlConnection sqlConn=new System.Data.SqlClient.SqlConnection("workstation id=LIUFPAD;packet size=4096;user id=sa;data source=LIUFPAD;persist security info=False;initial catalog=pubs");
//要得到的数据为authors表中的姓氏和名字
string sConn="Select au_lname,au_fname from authors";
myds=new DataSet();
System.Data.SqlClient.SqlDataAdapter sqlAd=new System.Data.SqlClient.SqlDataAdapter(sConn,sqlConn);
//从数据库中取得数据放入内存DataSet对象中,并映射为Authors表
sqlAd.Fill(myds,"authors");
//保存DataSet对象于Session变量MyData中
Session["MyData"]=myds;
}
catch(Exception ex)
{
string s=ex.Message;
}
}
else
{
myds=(DataSet)Session["MyData"];
if(myds==null)
this.Response.Write("无法获取数据");
else
{
switch(this.DpDnLst.SelectedItem.Text)
{
case "Repeater":
this.Response.Write("<center>以<I>Repeater</I>控件显示数据</center>");
this.db1.DataSource=myds.Tables["authors"].DefaultView;
db1.DataBind();
this.panel1.Visible=true;
this.panel2.Visible=false;
this.panel3.Visible=false;
break;
case "DataList":
this.Response.Write("<center>以<I>DataList</I>控件显示数据</center>");
this.db2.DataSource=myds.Tables["authors"].DefaultView;
db2.DataBind();
this.panel1.Visible=false;
this.panel2.Visible=true;
this.panel3.Visible=false;
break;
case "DataGrid":
this.Response.Write("<center>以<I>DataGrid</I>控件显示数据</center>");
this.db3.DataSource=myds.Tables["authors"].DefaultView;
db3.DataBind();
this.panel1.Visible=false;
this.panel2.Visible=false;
this.panel3.Visible=true;
this.db3.Visible=true;
break;
}
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -