📄 admingonggao.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
string myconn=System .Configuration .ConfigurationManager .AppSettings ["ConnectionString"];
conn = new SqlConnection(myconn );
if (!IsPostBack)
{
if (Request.Cookies["useName"] == null)
{
conn.Open();
string sql = "select * from gonggao";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
try
{ //身份验证成功
if (dr.Read())
{
Label1.Text = dr["g_time"].ToString();
Label1.Style["color"] = "black";
TextBox1.Text = dr["g_info"].ToString();
}
}
finally
{
dr.Close();
conn.Close();
}
}
else
{
Response.Redirect("~/Login.aspx");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string ginfo = TextBox1.Text;
string gtime = DateTime.Now.ToString();
//string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["ElectConnectionString"]);
// SqlConnection cn = new SqlConnection(settings);
conn.Open();
string mysql1 = "UPDATE gonggao SET g_info='" + ginfo + "', g_time='" + gtime + "' WHERE g_ID=1";
SqlCommand cm = new SqlCommand(mysql1, conn );
cm.CommandType = CommandType.Text;
try
{
cm.ExecuteNonQuery();
Label2.Text = "提示:更新成功!";
Label2.Style["color"] = "red";
}
catch (SqlException)
{
Label2.Text = "提示:更新失败!";
Label2.Style["color"] = "red";
}
conn.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
conn.Open();
string mysql2 = "UPDATE gonggao SET g_state='关闭' WHERE g_ID=1";
SqlCommand cm = new SqlCommand(mysql2, conn );
cm.CommandType = CommandType.Text;
try
{
cm.ExecuteNonQuery();
Label2.Text = "提示:关闭成功!";
Label2.Style["color"] = "red";
Button3.Visible = true;
Button2.Visible = true ;
}
catch (SqlException)
{
Label2.Text = "提示:关闭失败!";
Label2.Style["color"] = "red";
}
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
conn.Open();
string mysql3 = "UPDATE gonggao SET g_state='启用' WHERE g_ID=1";
SqlCommand cm = new SqlCommand(mysql3, conn);
cm.CommandType = CommandType.Text;
try
{
cm.ExecuteNonQuery();
Label2.Text = "提示:启用成功!";
Label2.Style["color"] = "red";
Button2.Visible = true;
Button3.Visible = true ;
}
catch (SqlException)
{
Label2.Text = "提示:启用失败!";
Label2.Style["color"] = "red";
}
conn.Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -