📄 lyb.c
字号:
using System;
using System.Data.SqlClient;
private void Page_Load(object sender, System.EventArgs e) //显示当前时间
{
int year=System.DateTime.Today.Year;
int month=System.DateTime.Today.Month;
int day=System.DateTime.Today.Day;
string xq=System.DateTime.Today.DayOfWeek.ToString();
string num=Application["count"].ToString();
lbtime.Text="今天是"+year+"年"+month+"月"+day+"日。";
lbxq.Text=xq;
lbmessage.Text="你是第"+num+"位访客。";
if(Session["uname"]==null)
{
this.bttuichu.Visible=false;
//this.lbmessage2.Text="你还没有登录!";
}
else
{
this.bttuichu.Visible=true;
//lbmessage2.Text="欢迎你"+Session["uname"].ToString();
}
if(!this.IsPostBack)
{
SqlConnection con=All.createCon();
con.Open();
DataSet ds=new DataSet();
SqlDataAdapter ads=new SqlDataAdapter("select * from
news",con);
ads.Fill(ds,"news");
this.DataList1.DataSource=ds.Tables["news"];
this.DataList1.DataBind();
SqlDataAdapter ads1=new SqlDataAdapter("select * from
users",con);
ads1.Fill(ds,"users");
this.DataList2.DataSource=ds.Tables["users"];
this.DataList2.DataBind();
SqlDataAdapter ads2=new SqlDataAdapter("select * from
words",con);
ads2.Fill(ds,"words");
this.DataGrid1.DataSource=ds.Tables["words"];
this.DataGrid1.DataBind();
con.Close();
}
private void btlogin_Click(object sender, System.EventArgs e)//连接登陆
{
bool yz=this.Request.Cookies["ImageV"].Value==txtcode.Text;
if(txtname.Text!=""&&txtpwd.Text!="")
{
SqlConnection con=All.createCon();
con.Open();
SqlCommand cmd=new SqlCommand("select * from users where
uname='"+txtname.Text+"'and upwd='"+txtpwd.Text+"'",con);
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
if(yz)
{
this.bttuichu.Visible=true;
string uname=txtname.Text;
string upwd=txtpwd.Text;
Session["uname"]=uname;
lbmessage2.Text+="欢迎你,"+uname;
txtname.Text="";
Page.RegisterClientScriptBlock
("loginno","<script>alert('登录成功!');</script>");
//System.Web.Security.FormsAuthentication.RedirectFromLoginPage(uname,true);
}
else
{
Page.RegisterClientScriptBlock
("loginno","<script>alert('验证码错误!');</script>");
}
}
else
{
Page.RegisterClientScriptBlock
("loginno","<script>alert('用户名或密码错误!');</script>");
txtname.Text="";
txtpwd.Text="";
}
con.Close();
}
else
{
Page.RegisterClientScriptBlock("loginno","<script>alert('用
户名或密码为空!');</script>");
}
}
private void btclear_Click(object sender, System.EventArgs e)
{
txtname.Text="";
txtpwd.Text="";
}
private void bttuichu_Click(object sender, System.EventArgs e)
{
Session["uname"]=null;
this.bttuichu.Visible=false;
this.lbmessage2.Text="你未登录!";
}
}
}
public void databind()
{
SqlConnection con=All.createCon();
con.Open();
SqlDataAdapter sda=new SqlDataAdapter("select top 5 jphone ,sum
(jmoney) from jk group by jphone order by sum(jmoney) desc ",con);
DataSet ds=new DataSet();
sda.Fill(ds,"jk");
this.DataGrid1.DataSource=ds.Tables["jk"];
this.DataGrid1.DataBind();
}
private void btjk_Click(object sender, System.EventArgs e)//捐款
{
string jphone=txtphone.Text;
int jmoney=(int)rblmoney.SelectedIndex+1;
string jtime=DateTime.Now.ToString();
SqlConnection con=All.createCon();
con.Open();
string strCom="insert into jk values
('"+jtime+"','"+jphone+"',"+jmoney+")";
SqlCommand com=new SqlCommand(strCom,con);
com.ExecuteNonQuery();
lbmessage.Text="恭喜你已经成功捐款"+jmoney+"元!";
SqlCommand cmd=new SqlCommand("select sum(jmoney) from jk",con);
int num=(int)cmd.ExecuteScalar();
lbmessage.Text+="目前共收到捐款"+num+"元!";
con.Close();
this.databind();
}
private void bttuichu_Click(object sender, System.EventArgs e)//退出登陆
{
if(Session["uname"]!=null)
{
Session["uanme"]=null;
}
else
{
Response.Redirect("index.aspx");
}
}
private void btsend_Click(object sender, System.EventArgs e)//留言板
{
SqlConnection con=All.createCon();
con.Open();
string name;
if(Session["uname"]!=null)
{
name=Session["uname"].ToString();
}
else
{
name="[未登录]"+this.txtname.Text;
}
string words=this.txtword.Text;
string time=DateTime.Now.ToString();
string strCom="insert into words values (' "+name+" ',' "+time+" ','
"+words+" ')";
SqlCommand cmd=new SqlCommand(strCom,con);
cmd.ExecuteNonQuery();
con.Close();
this.databind();
txtname.Text="";
txtword.Text="";
}
private void Page_Load(object sender, System.EventArgs e)//判断须先登陆才可访问
{
if(!IsPostBack)
{
if(Session["uname"]==null)
{
Page.RegisterClientScriptBlock
("loginno","<script>alert('对不起,请你先登录!');</script>");
Response.Redirect("index.aspx");
}
else
{
string username=Session["uname"].ToString();
bool ll=All.testuser(username);//检测是否为管理员
if(ll)
{
this.btupdate.Enabled=false;
this.btdelete.Enabled=false;
lbnum.Text="";
this.databind();
}
else
{
Page.RegisterClientScriptBlock
("loginno","<script>alert('对不起,你没权浏览此页!');</script>");
Response.Redirect("index.aspx");
}
}
}
}
public void databind()
{
SqlConnection con=All.createCon();
con.Open();
SqlDataAdapter sda=new SqlDataAdapter("select * from news",con);
DataSet ds=new DataSet();
sda.Fill(ds,"news");
this.DataGrid1.DataKeyField="nid";
this.DataGrid1.DataSource=ds.Tables["news"];
this.DataGrid1.DataBind();
}
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)//连接数据库,读取数据
{
string nid=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con=All.createCon();
con.Open();
SqlCommand cmd=new SqlCommand("delete from words where
nid="+nid,con);
cmd.ExecuteNonQuery();
con.Close();
this.databind();
}
public void databind()
{
SqlConnection con=All.createCon();
con.Open();
SqlDataAdapter sda=new SqlDataAdapter("select * from shehui",con);
DataSet ds=new DataSet();
sda.Fill(ds,"shehui");
this.DataGrid1.DataSource=ds.Tables["shehui"];
this.DataGrid1.DataBind();
}
private void ddltitle_SelectedIndexChanged(object sender, System.EventArgs e)//投票
调查
{
this.tid=Convert.ToInt32(this.ddltitle.SelectedValue.ToString());
if(tid==1)
{
this.Label1.Text="欢迎进行捐款投票";
}
else
{
this.Label1.Text="欢迎进行网站意见投票";
}
this.listBind(tid);
this.DataGrid1.Visible=false;
}
private void listBind(int tid)
{
SqlConnection con=All.createCon();
con.Open();
SqlCommand cmditem=new SqlCommand("select * from tpitem where
tid="+tid,con);
SqlDataReader reader=cmditem.ExecuteReader();
this.rblmember.DataSource=reader;
this.rblmember.DataValueField="pid";
this.rblmember.DataTextField="pitem";
this.rblmember.DataBind();
reader.Close();
}
private void btvote_Click(object sender, System.EventArgs e)
{
this.DataGrid1.Visible=false;
this.tid=Convert.ToInt32(this.ddltitle.SelectedValue.ToString());
SqlConnection con=All.createCon();
con.Open();
SqlCommand cmd=new SqlCommand("update tpitem set pnum=pnum+1 where
pid="+this.rblmember.SelectedValue+"and tid="+tid,con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script language='javascript'>alert('投票成功')
</script>");
}
private void bt_Click(object sender, System.EventArgs e)
{
this.DataGrid1.Visible=true;
int scantid=Convert.ToInt32(this.ddltitle.SelectedValue);
Response.Write(scantid);
SqlConnection con=All.createCon();
con.Open();
SqlDataAdapter sda=new SqlDataAdapter("select pitem , pnum from
tpitem where tid="+scantid,con);
DataSet ds=new DataSet();
sda.Fill(ds,"tpitem");
this.DataGrid1.DataSource =ds.Tables["tpitem"];
this.DataGrid1.DataBind();
}
}
}
this.DataList2.DataKeyField="uname";
this.DataList2.DataSource=ds.Tables["users"];
this.DataList2.DataBind();
string unamell=Session["uname"].ToString();//登录的用户
string uname=txtname.Text;//被浏览的用户名
bool ll=All.testuser(unamell);//检测是否为管理员
if(ll)//是管理员
{
this.Checkgl.Enabled=true;
}
else
{
this.Checkgl.Enabled=false;
}
}
private void btchange_Click(object sender, System.EventArgs e)
{
string unamell=Session["uname"].ToString();//登录的用户
string uname=txtname.Text;//被浏览的用户名
bool ll=All.testuser(unamell);//检测是否为管理员
if(ll)//是管理员
{
SqlConnection con=All.createCon();
con.Open();
string usex;
if(rbtmale.Checked)
{
usex="男";
}
else
{
usex="女";
}
string uphone=txtphone.Text;
string umail=txtmail.Text;
string uaddress=txtaddress.Text;
string strCom="update users set
usex='"+usex+"',uphone='"+uphone+"',umail='"+umail+"',uaddress='"+uaddress;
if(this.Checkchange.Checked)
{
string upwd=txtnewpwd.Text;
strCom="update users set
upwd='"+upwd+"',usex='"+usex+"',uphone='"+uphone+"',umail='"+umail+"',uaddress='"+
uaddress+"'where uname='"+uname;
}
if(this.Checkgl.Checked)
{
strCom+="',uzhiwu='管理员";
}
strCom+="'where uname='"+uname+"'";
SqlCommand com=new SqlCommand(strCom,con);
com.ExecuteNonQuery();
con.Close();
string message="管理员"+unamell+"已成功修改"+uname+"的资料!
";
Page.RegisterClientScriptBlock("loginno",message);
}
else//不是管理员
{
if(Session["uname"].ToString()==txtname.Text)//修改自己的资
料
{
SqlConnection con=All.createCon();
con.Open();
string usex;
if(rbtmale.Checked)
{
usex="男";
}
else
{
usex="女";
}
string uphone=txtphone.Text;
string umail=txtmail.Text;
string uaddress=txtaddress.Text;
string strCom="update users set
usex='"+usex+"',uphone='"+uphone+"',umail='"+umail+"',uaddress='"+uaddress;
if(this.Checkchange.Checked)
{
string upwd=txtnewpwd.Text;
strCom="update users set
upwd='"+upwd+"',usex='"+usex+"',uphone='"+uphone+"',umail='"+umail+"',uaddress='"+
uaddress+"'where uname='"+uname;
}
strCom+="'where uname='"+uname+"'";
SqlCommand com=new SqlCommand(strCom,con);
com.ExecuteNonQuery();
con.Close();
string message=uname+",你已经成功修改了你的个人资料
!";
Page.RegisterClientScriptBlock("loginno",message);
}
else//修改别人的信息
{
Page.RegisterClientScriptBlock
("loginno","<script>alert('对不起,你没权修改别人的资料!');</script>");
}
}
}
private void Checkchange_CheckedChanged(object sender, System.EventArgs e)
{if(this.Checkchange.Checked)
{this.Panel1.Visible=true;}
else
this.Panel1.Visible=false;
}
private void btsearch_Click(object sender, System.EventArgs e)
{
string username=this.txtserchname.Text;
this.Panel3.Visible=false;
this.Panel2.Visible=true;
SqlConnection con=All.createCon();
con.Open();
SqlCommand com=new SqlCommand("select * from users where
uname='"+username+"'",con);
SqlDataReader reader=com.ExecuteReader();
if(reader.Read())
{txtname.Text=username;
txtpwd.Text=reader.GetValue(1).ToString();
txtphone.Text=reader.GetValue(3).ToString().Trim();
txtmail.Text=reader.GetValue(4).ToString().Trim();
txtaddress.Text=reader.GetValue(5).ToString();
if(reader.GetValue(2).ToString()=="男")
{
rbtmale.Checked=true;
}
else
{
rbtfemale.Checked=true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -