제목 : 출력 페이지 클래스 작성 : /BasicCS/List.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 BasicCS
{
/// <summary>
/// List에 대한 요약 설명입니다.
/// </summary>
public class List : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button btnWrite;
protected System.Web.UI.WebControls.TextBox SearchQuery;
protected System.Web.UI.WebControls.DropDownList SearchField;
protected System.Web.UI.WebControls.Button btnSearch;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
private void Page_Load(object sender, System.EventArgs e)
{
if(Request.Cookies["Basic"] != null)
{
string strPageNum = Request.Cookies["Basic"]["PageNum"].ToString();
if(strPageNum != null)
{
DataGrid1.CurrentPageIndex = Convert.ToInt32(strPageNum);
}
}
if(!Page.IsPostBack)
{
ReadData();
}
}
private void ReadData()//데이터 읽어서 데이터그리드에 출력하는 함수(메서드)
{
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString = ConfigurationSettings.AppSettings["CONNECTION_STRING"];
objCon.Open();
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
objCmd.CommandText = "procListBasic";
objCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter objDa = new SqlDataAdapter();
objDa.SelectCommand = objCmd;
DataSet objDs = new DataSet();
objDa.Fill(objDs, "Basic");
DataGrid1.DataSource = objDs.Tables["Basic"].DefaultView;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 이 호출은 ASP.NET Web Form 디자이너에 필요합니다.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
this.btnWrite.Click += new System.EventHandler(this.btnWrite_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnWrite_Click(object sender, System.EventArgs e)
{
Response.Redirect("./Write.aspx");
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
Response.Cookies["Basic"]["PageNum"] = e.NewPageIndex.ToString();//페이지 번호 유지
ReadData();
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
string strQuery = "./Search.aspx?SearchField=" +
SearchField.SelectedItem.Value +
"&SearchQuery=" + SearchQuery.Text;
Response.Redirect(strQuery);
}
}
}