using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
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 hualong
{
/// <summary>
/// scxxxx 的摘要说明。
/// </summary>
public class scxxxx : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgHaoMa;
protected Wuqi.Webdiyer.AspNetPager AspNetPager1;
public int jiLuCount;//记录总数
public int youXiaoShu;//有效卡总数
public EnClass En = new EnClass();//初始化一个加密类的对象
//OleDbConnection conn;
string piCi;//传递过来的字符串
public Hashtable hash;//一个Hashtable,用来存储数据表中的有效字段值,即dListYouXiao值
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//首先给初始化hash表并且赋值
hash = new Hashtable();
hash["有效"] = 0;
hash["无效"] = 1;
piCi = Request.QueryString["id"].ToString();//获得传递过来的字符串一做查询条件
if(!IsPostBack)//这里一定要写这个if语句,不然更新数据的时候还是老数据,一定!!!!
{
BindData();//调用DataBind方法,绑定数据和分页
}
}
/// <summary>
/// GetIndex()方法,用他来返回dListYouXiao下拉列表框的索引
/// </summary>
/// <param name="str">参数str为传递过来的文本值</param>
/// <returns>根据传递过来的文本值返回这个值在下拉列表框中的索引</returns>
public int GetIndex(string str)
{
if(null!=hash[str])
return (int)hash[str];//如果传递来的文本在hash中存在则返回他的值
else
return 0;
}
//绑定数据
private void BindData()
{
string connString = ConfigurationSettings.AppSettings["connStr"].ToString() + Server.MapPath(Request.ApplicationPath) +"//hualong.mdb";
OleDbConnection conn = new OleDbConnection(connString);
string selString = "select * from haoma where pici = @pici";
string selString1 = string.Format("select Count(*) from haoma where pici = '{0}'",piCi);
string selString2 = string.Format("select Count(*) from haoma where pici = '{0}' and youxiao = '有效'",piCi);
OleDbCommand comm = new OleDbCommand(selString1,conn);
conn.Open();
AspNetPager1.RecordCount = (int)comm.ExecuteScalar();
jiLuCount = AspNetPager1.RecordCount;//取得所卡的总数
conn.Close();
OleDbCommand comm2 = new OleDbCommand(selString2,conn);
conn.Open();
youXiaoShu = (int)comm2.ExecuteScalar();//取得所有有效卡的总数
conn.Close();
OleDbDataAdapter adapter = new OleDbDataAdapter(selString,conn);
adapter.SelectCommand.Parameters.Add(new OleDbParameter("@pici",OleDbType.VarChar,50));
adapter.SelectCommand.Parameters["@pici"].Value = piCi;
DataSet ds = new DataSet();
adapter.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"haoma");
dgHaoMa.DataSource =ds.Tables["haoma"].DefaultView;
dgHaoMa.DataBind();
//分页控件的用户自定义信息必须在这里写才能够显示
AspNetPager1.CustomInfoText="记录总数:<font color=/"blue/"><b>"+AspNetPager1.RecordCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 总页数:<font color=/"blue/"><b>"+AspNetPager1.PageCount.ToString()+"</b></font>";
AspNetPager1.CustomInfoText+=" 当前页:<font color=/"red/"><b>"+AspNetPager1.CurrentPageIndex.ToString()+"</b></font>";
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgHaoMa.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgHaoMa_CancelCommand);
this.dgHaoMa.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgHaoMa_EditCommand);
this.dgHaoMa.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgHaoMa_UpdateCommand);
this.dgHaoMa.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgHaoMa_DeleteCommand);
this.AspNetPager1.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.AspNetPager1_PageChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData();
}
private void dgHaoMa_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgHaoMa.EditItemIndex = e.Item.ItemIndex;//让按下编辑按钮的那行处于编辑状态
BindData();//一定要再次进行数据绑定
}
private void dgHaoMa_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgHaoMa.EditItemIndex = -1;//让dgHaoMa处于非编辑状态
BindData();//一定要进行数据绑定
}
private void dgHaoMa_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string connString = ConfigurationSettings.AppSettings["connStr"].ToString() + Server.MapPath(Request.ApplicationPath) +"//hualong.mdb";
OleDbConnection conn = new OleDbConnection(connString);
string delString = "delete from haoma where kahao = @kahao";//因为kahao字段是DataKeyField主索引键,我们根据他来删除数据行
OleDbCommand comm = new OleDbCommand(delString,conn);
comm.Parameters.Add(new OleDbParameter("@kahao",OleDbType.VarChar,50));
//首先取得当前行的主键值,这里是卡号,不要加密
comm.Parameters["@kahao"].Value = dgHaoMa.DataKeys[e.Item.ItemIndex];
conn.Open();
try
{
comm.ExecuteNonQuery();
}
catch
{
Response.Write("删除失败!");
}
conn.Close();
BindData();
}
private void dgHaoMa_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string connString = ConfigurationSettings.AppSettings["connStr"].ToString() + Server.MapPath(Request.ApplicationPath) +"//hualong.mdb";
OleDbConnection conn = new OleDbConnection(connString);
// 建立 UPDATE 命令字串
string upString = "UPDATE haoma set youxiao = @youxiao where kahao = @kahao";
OleDbCommand comm = new OleDbCommand(upString,conn);
//添加comm的参数
comm.Parameters.Add(new OleDbParameter("@youxiao",OleDbType.VarChar,50));
comm.Parameters.Add(new OleDbParameter("@kahao",OleDbType.VarChar,50));
//给comm参数赋值,首先取得当前行的主键值,这里是卡号,不要对其加密
comm.Parameters["@kahao"].Value = dgHaoMa.DataKeys[e.Item.ItemIndex];
//因为dListYouXiao属于dgHaoma的内置控件,我们首先必须通过FindControl方法来找到他
DropDownList dListYouXiao;
dListYouXiao = (DropDownList)e.Item.FindControl("dListYouXiao");
//找到了控件再给comm参数赋值
comm.Parameters["@youxiao"].Value = dListYouXiao.SelectedItem.Text.ToString();
//开启conn
comm.Connection.Open();
try
{
comm.ExecuteNonQuery();
Response.Write("<font color=/"red/">已经成功更新了数据!</font>");
//使dgHaoMa处于非编辑状态
dgHaoMa.EditItemIndex = -1;
}
catch
{
Response.Write("<font color=/"red/">错误:无法更新数据!</font>");
}
conn.Close();
BindData();//一定要再次绑定
}
}
}