📄 guestbook_main.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;
using System.Configuration;
namespace baogelyb
{
/// <summary>
/// guestbook_main 的摘要说明。
/// </summary>
public class guestbook_main : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label name;
protected System.Web.UI.WebControls.Label content;
protected System.Web.UI.WebControls.TextBox replytime;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.TextBox replycontent;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
private void Page_Load(object sender, System.EventArgs e)
{
if(Session["name"]==null||Session["name"].ToString()=="false")
{
Response.Redirect("fail.htm");
}
if(!IsPostBack)
{
CDataBind();
}
}
private void CDataBind()
{
// string Addtime=DateTime.Now.ToString().Substring(0,10);
int CID=Convert.ToInt32(Request.QueryString["id"].Trim());
//取得数据库路径
// string dbPath=System.Configuration.ConfigurationSettings.AppSettings["lyb"];
// //定义连接字符串
// string ACconnStr="Provider=Microsoft.Jet.Sql.4.0;Data Source="+Server.MapPath(dbPath);
//创建数据库对像
SqlConnection ACconn=db.createconnection();
//数据库操作
try
{
ACconn.Open();
//取得所需操作的记录ID
//定义SQL语句
string ACsql="select * from lyb where id="+CID;
//创建Command对像
SqlCommand ACcomm=new SqlCommand(ACsql,ACconn);
//通过执行Command实例化DataReader类
SqlDataReader ACdr=ACcomm.ExecuteReader();
if(ACdr.Read())
{
this.name.Text=ACdr["name"].ToString();
this.content.Text=ACdr["content"].ToString();
this.replytime.Text=ACdr["replytime"].ToString();
this.replycontent.Text=ACdr["replycontent"].ToString().Replace("<br>","\r\n");
// this.Addtime.Text=ACdr["Addtime"].ToString().Substring(0,8);
// this.Addtime.Text=ACdr["Addtime"].ToString("yyyy-MM-dd");
ACdr.Close();
}
else
{
Response.Write("对不起,请先填加留言");
}
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
ACconn.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//取得数据库路径
// string dbPath=System.Configuration.ConfigurationSettings.AppSettings["lyb"];
// //定义连接字符串
// string ACconnStr="Provider=Microsoft.Jet.Sql.4.0;Data Source="+Server.MapPath(dbPath);
//创建数据库连接对像
SqlConnection ACconn=db.createconnection();
//操作数据库
try
{
int CID=Convert.ToInt32(Request.QueryString["id"].Trim());
ACconn.Open();
//定义所需SQL
string ACsql="Update lyb set name='"+name.Text+"',content='"+this.content.Text+"',replycontent='"+this.replycontent.Text.ToString().Replace("\r\n","<br>")+"',replytime='"+this.replytime.Text+"' where id="+CID;
//创建Command对像
SqlCommand ACcomm=new SqlCommand(ACsql,ACconn);
ACcomm.ExecuteNonQuery();
Response.Write("<script>alert('留言回复成功');</script>");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
ACconn.Close();
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
this.replytime.Text="";
this.replycontent.Text="";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -