📄 liulanclient.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 login
{
/// <summary>
/// liulanclient 的摘要说明。
/// </summary>
public class liulanclient : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
this.Bind();
}// 在此处放置用户代码以初始化页面
}
public void Bind()
{
SqlConnection con=odb.createCon();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from yonghu",con);
DataSet ds=new DataSet();
sda.Fill(ds,"yonghu");
this.DataGrid1.DataSource=ds.Tables["yonghu"];
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.Bind();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('你确认删除吗?');");
}
}
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(ViewState["order"]==null)
{
ViewState["order"]="ASC";
}
else
{
if(ViewState["order"].ToString()=="ASC")
{
ViewState["order"]="DESC";
}
else
{
ViewState["order"]="ASC";
}
}
SqlConnection con=odb.createCon();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from yonghu",con);
DataSet ds=new DataSet();
sda.Fill(ds,"yonghu");
ds.Tables["yonghu"].DefaultView.Sort=e.SortExpression+" "+ViewState["order"].ToString();
this.DataGrid1.DataSource=ds.Tables["yonghu"].DefaultView;
this.DataGrid1.DataBind();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection con=odb.createCon();
SqlCommand cm=new SqlCommand("delete from yonghu where userid=@userid",con);
cm.Parameters.Add(new SqlParameter("@userid",SqlDbType.Char,10));
// this.DataGrid1.DataKeyField="userid";
cm.Parameters["@userid"].Value=(e.Item.Cells[0]).Text.ToString();
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
this.Bind();
}
catch(SqlException)
{
this.Label1.Text="删除失败";
}
cm.Connection.Close();
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
this.Bind();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
this.Bind();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection con=odb.createCon();
SqlCommand cm=new SqlCommand("updateyonghu",con);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@userid",SqlDbType.Char,10));
cm.Parameters.Add(new SqlParameter("@username",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@pwd",SqlDbType.VarChar,20));
cm.Parameters.Add(new SqlParameter("@bumen",SqlDbType.Int));
cm.Parameters.Add(new SqlParameter("@tell",SqlDbType.VarChar,20));
cm.Parameters.Add(new SqlParameter("@mail",SqlDbType.VarChar,50));
string col=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@username"].Value=col;
col=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@pwd"].Value=col;
col=((TextBox)e.Item.Cells[3].Controls[0]).Text;
cm.Parameters["@bumen"].Value=col;
col=((TextBox)e.Item.Cells[4].Controls[0]).Text;
cm.Parameters["@tell"].Value=col;
col=((TextBox)e.Item.Cells[5].Controls[0]).Text;
cm.Parameters["@mail"].Value=col;
cm.Parameters["@userid"].Value=((TextBox)e.Item.Cells[0].Controls[0]).Text;
cm.Connection.Open();
cm.ExecuteNonQuery();
cm.Connection.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -