📄 index.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
using System.Data.SqlTypes;
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.Web.Mail;
namespace Gbook
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class Index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label txtMessage;
protected System.Web.UI.WebControls.TextBox GM_NAME;
protected System.Web.UI.WebControls.TextBox GM_EMAIL;
protected System.Web.UI.WebControls.TextBox GM_HOMEPAGE;
protected System.Web.UI.WebControls.TextBox GM_OICQ;
protected System.Web.UI.WebControls.TextBox GM_MSN;
protected System.Web.UI.WebControls.TextBox GM_CONTENT;
protected System.Web.UI.WebControls.DropDownList GM_AREA;
protected System.Web.UI.WebControls.CheckBox GM_REPLY_EMAIL;
protected System.Web.UI.WebControls.RadioButtonList GM_FACE;
protected System.Web.UI.WebControls.Button btnSubmit;
protected System.Web.UI.WebControls.Button btnReset;
protected System.Web.UI.WebControls.TextBox KeyWords;
protected System.Web.UI.WebControls.Button btnsearch;
protected System.Web.UI.WebControls.RequiredFieldValidator RF_NAME;
protected System.Web.UI.WebControls.RequiredFieldValidator RF_EMAIL;
protected System.Web.UI.WebControls.RegularExpressionValidator RE_EMAIL;
protected System.Web.UI.WebControls.RegularExpressionValidator RE_HOMEPAGE;
protected System.Web.UI.WebControls.RegularExpressionValidator RF_OICQ;
protected System.Web.UI.WebControls.RegularExpressionValidator RE_MSN;
protected System.Web.UI.WebControls.RequiredFieldValidator RF_MESSAGE;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private Gbook.Conn myConn = new Gbook.Conn(); // [打开数据库]
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (KeyWords.Text.ToString().Trim() !="") {
BindDataGrid(0);
}
else
{
if(!Page.IsPostBack)
{
BindDataGrid(0);
}
}
}
/// <summary>
/// 获取数据库连接
/// </summary>
private string strConn
{
get { return myConn.ConnString();}
}
private void BindDataGrid(int intPageNumber)
{
// ----------- 数据库连接 -----------
SqlConnection myConnection = new SqlConnection(strConn);
myConnection.Open();
string SqlCmd;
SqlCmd = "Select * From GuestBook_Message";
if (KeyWords.Text.ToString().Trim() !="")
SqlCmd += " Where (GM_CONTENT like '%" + KeyWords.Text.ToString().Trim() + "%') ";
SqlCmd += " Order By GM_ID Desc";
/* // 方法一
SqlCommand myCommand = new SqlCommand(SqlCmd, myConnection);
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = myCommand;
DataSet ds1= new DataSet();
sqlDataAdapter1.SelectCommand.ExecuteNonQuery();
sqlDataAdapter1.Fill(ds1);
DataView source = new DataView(ds1.Tables[0]);
DataGrid1.DataSource=source;
DataGrid1.DataBind();
myConnection.Close();
*/
// 方法二
SqlDataAdapter DataAdapter1 = new SqlDataAdapter(SqlCmd, myConnection);
DataSet ds = new DataSet();
DataAdapter1.Fill(ds,"GBookList");
try
{
DataTable GBookList = ds.Tables["GBookList"];
DataGrid1.DataSource = GBookList;
/*-----------------------------------------
* 翻页的时候要设置的就是下面这个属性
* ----------------------------------------*/
DataGrid1.CurrentPageIndex = intPageNumber;
DataGrid1.DataBind();
for(int i=0;i<DataGrid1.Items.Count;i++)
{
string strTmpTitle = "";
string strEmail = GBookList.Rows[i]["GM_EMAIL"].ToString().Trim();
string strHomePage = GBookList.Rows[i]["GM_HOMEPAGE"].ToString().Trim();
string strArea = GBookList.Rows[i]["GM_AREA"].ToString().Trim();
string strOICQ = GBookList.Rows[i]["GM_OICQ"].ToString().Trim();
string strMsn = GBookList.Rows[i]["GM_MSN"].ToString().Trim();
string strReTime = GBookList.Rows[i]["GM_RETIME"].ToString().Trim();
string strReply = GBookList.Rows[i]["GM_REPLY"].ToString().Trim();
if (strEmail!="")
strTmpTitle = "<a title='邮箱:"+ strEmail +"' href='mailto:"+ strEmail +"' class='char1'><font class='size'>Email</font></a> ";
if (strHomePage!="")
strTmpTitle += "<a title='主页:"+ strHomePage +"' href='"+ strHomePage +"' class='char1' target='_blank'><font class='size'>Homepage</font></a> ";
if (strArea!="")
strTmpTitle += "<a title='来自"+ strArea +"' href='javascript:来自"+ strArea +"' onclick='return false;' class='char1'><font class='size'>From</font></a> ";
if (strOICQ!="")
strTmpTitle += "<a title='OICQ:"+ strOICQ +"' href='http://search.tencent.com/cgi-bin/friend/user_show_info?ln="+ strOICQ +"' class='char1' target='_blank'><font class='size'>Oicq</font></a> ";
if (strMsn!="")
strTmpTitle += "<a title='MSN:"+ strMsn +"' href='mailto:"+ strMsn +"' class='char1'><font class='size'>MSN</font></a> ";
if (strReply!="")
{
((System.Web.UI.HtmlControls.HtmlTableRow)DataGrid1.Items[i].FindControl("ItemTr1")).Visible = true;
((System.Web.UI.HtmlControls.HtmlTableRow)DataGrid1.Items[i].FindControl("ItemTr2")).Visible = true;
}
if (strReTime!="")
{
((Label)DataGrid1.Items[i].FindControl("ReplyTime")).Visible = true;
((Label)DataGrid1.Items[i].FindControl("ReplyTime")).Text = " <b>Retime:</b>" + strReTime;
}
((Label)DataGrid1.Items[i].FindControl("ItemTitle")).Text = strTmpTitle;
// ((Label)DataGrid1.Items[i].Cells[0].FindControl("ItemTitle")).Text = strTmpTitle;
// ((Label)DataGrid1.Items[i].FindControl("ItemTitle")).Text = strTmpTitle;
// Response.Write(strTmpTitle + "<br>");
}
}
catch (SqlException ex)
{
txtMessage.Visible = true;
txtMessage.Text = ex.Message;
}
finally
{
ds.Dispose();
ds.Clear();
DataAdapter1.Dispose();
myConnection.Close();
}
}
private void btnSubmit_Click(object sender, System.EventArgs e)
{
string Error,REPLY_EMAIL,txtHOMEPAGE,txtCONTENT,GM_IP;
Error = "";
GM_IP = Request.ServerVariables["REMOTE_HOST"];
if(GM_REPLY_EMAIL.Checked) REPLY_EMAIL = "1";
else REPLY_EMAIL = "0";
if(GM_HOMEPAGE.Text=="http://") txtHOMEPAGE = "";
else txtHOMEPAGE = GM_HOMEPAGE.Text;
txtCONTENT = GM_CONTENT.Text.ToString().Trim().Replace("'","''").Replace("\n","<br>");
DateTime NowTime = DateTime.Now;
// Insert 语句
string SqlCmd = "INSERT INTO GuestBook_Message(GM_FACE,GM_NAME,GM_EMAIL,GM_HOMEPAGE,GM_AREA,GM_OICQ,GM_MSN,GM_CONTENT,GM_REPLY_EMAIL,GM_ADDTIME,GM_IP) Values ";
SqlCmd += "('" + GM_FACE.SelectedValue + "','" + GM_NAME.Text + "','" + GM_EMAIL.Text + "','" + GM_HOMEPAGE.Text + "','" + GM_AREA.SelectedValue + "','" + GM_OICQ.Text + "','" + GM_MSN.Text + "','" + txtCONTENT + "','" + REPLY_EMAIL + "','" + NowTime + "','" + GM_IP + "')";
// ----------- 数据库连接 -----------
SqlConnection myConnection = new SqlConnection(strConn);
SqlCommand myCommand = new SqlCommand(SqlCmd, myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
if (REPLY_EMAIL == "1") {
MailMessage message = new MailMessage();
message.BodyFormat = MailFormat.Text;
message.From = "1studio@sina.com";
message.To = GM_EMAIL.Text.Trim();
message.Subject = "测试邮件";
message.Body = txtCONTENT;
try
{
SmtpMail.SmtpServer = "smtp.sina.com.cn";
SmtpMail.Send(message);
}
catch(Exception ex)
{
Error = ex.Message;
}
}
}
catch(SqlException ex)
{
txtMessage.Visible = true;
Error = ex.Message;
}
if (Error.Length > 0)
{
txtMessage.Text = Error;
}
else
{
txtMessage.Visible = true;
txtMessage.Text = "留言成功!";
}
myConnection.Close();
BindDataGrid(0);
GM_NAME.Text = "";
GM_EMAIL.Text = "";
GM_HOMEPAGE.Text= "";
GM_OICQ.Text = "";
GM_MSN.Text = "";
GM_CONTENT.Text = "";
GM_AREA.SelectedValue = "";
GM_REPLY_EMAIL.Checked = false;
}
private void btnReset_Click(object sender, System.EventArgs e)
{
GM_NAME.Text = "";
GM_EMAIL.Text = "";
GM_HOMEPAGE.Text= "";
GM_OICQ.Text = "";
GM_MSN.Text = "";
GM_CONTENT.Text = "";
GM_AREA.SelectedValue = "";
GM_REPLY_EMAIL.Checked = false;
}
private void ChangePage(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
BindDataGrid(e.NewPageIndex);
}
#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.ChangePage);
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -