📄 buyingview.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;
namespace BookManager
{
/// <summary>
/// BuyingView 的摘要说明。
/// </summary>
public class BuyingView : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlAdapter;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Web.UI.WebControls.DataGrid dgBuyingView;
protected System.Web.UI.WebControls.Button btBack;
protected System.Web.UI.WebControls.Button btSumbit;
protected BookManager.dsBuyingView dsBuyingViewInfo;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
ViewState["BackUrl"]=Request.UrlReferrer.ToString();
this.DataBinds();
if(dgBuyingView.Items.Count==0)
{
Response.Redirect("NoData.aspx");
}
}
}
private void DataBinds()
{
sqlAdapter.Fill(dsBuyingViewInfo);
dgBuyingView.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlAdapter = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConn = new System.Data.SqlClient.SqlConnection();
this.dsBuyingViewInfo = new BookManager.dsBuyingView();
((System.ComponentModel.ISupportInitialize)(this.dsBuyingViewInfo)).BeginInit();
this.dgBuyingView.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgBuyingView_ItemCreated);
this.dgBuyingView.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgBuyingView_ItemCommand);
this.dgBuyingView.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgBuyingView_DeleteCommand);
this.btBack.Click += new System.EventHandler(this.btBack_Click);
//
// sqlAdapter
//
this.sqlAdapter.SelectCommand = this.sqlSelectCommand1;
this.sqlAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "tblBookStorage", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("Storage", "Storage"),
new System.Data.Common.DataColumnMapping("BookName", "BookName"),
new System.Data.Common.DataColumnMapping("Author", "Author"),
new System.Data.Common.DataColumnMapping("Price", "Price"),
new System.Data.Common.DataColumnMapping("Publish", "Publish"),
new System.Data.Common.DataColumnMapping("BookMark", "BookMark"),
new System.Data.Common.DataColumnMapping("Attribute", "Attribute"),
new System.Data.Common.DataColumnMapping("BookAttribute", "BookAttribute"),
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Expr1", "Expr1"),
new System.Data.Common.DataColumnMapping("Expr2", "Expr2")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = @"SELECT tblBookStorage.Storage, tblBookInfo.BookName, tblBookInfo.Author, tblBookInfo.Price, tblBookInfo.Publish, tblBookInfo.BookMark, tblBookInfo.Attribute, tblBookAttribute.BookAttribute, tblBookAttribute.ID, tblBookInfo.ID AS Expr1, tblBookStorage.ID AS Expr2 FROM tblBookStorage INNER JOIN tblBookInfo ON tblBookStorage.BookId = tblBookInfo.ID INNER JOIN tblBookAttribute ON tblBookInfo.Attribute = tblBookAttribute.ID WHERE (tblBookInfo.Attribute = 3)";
this.sqlSelectCommand1.Connection = this.sqlConn;
//
// sqlConn
//
this.sqlConn.ConnectionString = "workstation id=QMX;packet size=4096;user id=sa;data source=\"QMX\\THUNDER\";persist " +
"security info=True;initial catalog=BookManager;password=gliu0307";
//
// dsBuyingViewInfo
//
this.dsBuyingViewInfo.DataSetName = "dsBuyingView";
this.dsBuyingViewInfo.Locale = new System.Globalization.CultureInfo("zh-CN");
this.btSumbit.Click += new System.EventHandler(this.btSumbit_Click);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dsBuyingViewInfo)).EndInit();
}
#endregion
private void btBack_Click(object sender, System.EventArgs e)
{
if(ViewState["BackUrl"]!=null)
{
Response.Redirect(ViewState["BackUrl"].ToString());
}
}
private void ExecSql(string sqlStr)
{
SqlCommand cmd=new SqlCommand(sqlStr,sqlConn);
sqlConn.Open();
cmd.ExecuteNonQuery();
sqlConn.Close();
}
private void dgBuyingView_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sqlStr;
e.Item.Attributes.Add("onclick", "return confirm('您真的要删除所选项吗?');");
sqlStr="delete from tblBookInfo where ID="+e.Item.Cells[0].Text.ToString();
this.ExecSql(sqlStr);
this.DataBinds();
}
private void dgBuyingView_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="UserDelete")
this.dgBuyingView_DeleteCommand(source,e);
}
private void dgBuyingView_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.EditItem:
case ListItemType.AlternatingItem:
Button myDeleteButton = (Button)e.Item.FindControl("btDelete");
myDeleteButton.Text = "删除";
myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除吗?');");
break;
}
}
private void btSumbit_Click(object sender, System.EventArgs e)
{
string sqlStr;
foreach(DataGridItem dgi in dgBuyingView.Items)
{
CheckBox cb=(CheckBox)dgi.FindControl("cbSelect");
if(cb.Checked)
{
sqlStr="update tblBookInfo set Attribute=1 where ID="+dgi.Cells[0].Text.ToString();
this.ExecSql(sqlStr);
}
}
this.DataBinds();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -