📄 myviewstate.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.Configuration;
using System.Data.SqlClient;
namespace Example_13_3
{
/// <summary>
/// Summary description for MyViewState.
/// </summary>
public class MyViewState : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button SetViewState;
protected System.Web.UI.WebControls.Label ViewStateMsg;
protected System.Web.UI.WebControls.DataGrid viewStateDG;
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///绑定DataGrid控件的数据
GetStaffs();
}
}
private void GetStaffs()
{
///从数据库中Tabs表获取所有记录
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();
///绑定DataGrid控件的数据
viewStateDG.DataSource = dr;
viewStateDG.DataBind();
///关闭读取器和数据库的链接
dr.Close();
myConnection.Close();
}
#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.SetViewState.Click += new System.EventHandler(this.SetViewState_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SetViewState_Click(object sender, System.EventArgs e)
{
if(viewStateDG.EnableViewState == true)
{
viewStateDG.EnableViewState = false;
ViewStateMsg.Text = "控件viewStateDG的ViewState=false";
}
else
{
viewStateDG.EnableViewState = true;
ViewStateMsg.Text = "控件viewStateDG的ViewState=true";
}
///绑定DataGrid控件的数据
GetStaffs();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -