2004-5-1+ 用visual studio.net写的留言板

vs.net在控件管理方面果然是方便的很,我是今天才开始用vs.net的,过几天熟练了就写一篇文章上来,下面的这个留言本除了没有分页功能外,其他的功能都齐备。就是界面和数据表简单了些,做练习嘛~~
程序虽小,遇到的问题也有很多,最麻烦的是我不知道怎么才可以管理repeater模板里面的控件的visible属性,大家可以看下面有一段用粉红色标识的判断语句,根本就是asp的办法 :(我在.net framework sdk文档里翻了半天都没有找到好的解决办法,唉……哪位高人有高招啊??
guestbook.aspx
<%@ Page language="c#" Codebehind="guestbook.aspx.cs" AutoEventWireup="false" Inherits="guest.guestbook" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>guestbook</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<P>
<!--login-->
<asp:panel id="panadmin" runat="server" Visible="False">
<asp:textbox id="adminid" runat="server"></asp:textbox>&nbsp;
<asp:requiredfieldvalidator id="idreq" runat="server" ControlToValidate="adminid" ErrorMessage="id error"></asp:requiredfieldvalidator>
<asp:textbox id="adminpd" runat="server" TextMode="Password"></asp:textbox>&nbsp;
<asp:requiredfieldvalidator id="pdreq" runat="server" ControlToValidate="adminpd" ErrorMessage="pd error"></asp:requiredfieldvalidator>
<asp:button id="adminenter" runat="server" Text="enter the admin"></asp:button>&nbsp;
</asp:panel>
<!--end login-->
<!--state&quit-->

<asp:label id="ck" runat="server"></asp:label>
<asp:LinkButton id="squit" runat="server" Visible="False">quit</asp:LinkButton>
<asp:LinkButton id="writego" runat="server">new words!</asp:LinkButton>&nbsp;
<asp:LinkButton id="logingo" runat="server">login!!</asp:LinkButton>
<!--end state&quit-->
</P>
<!--repeater-->
<P>
<asp:repeater id="guestbody" runat="server">
<ItemTemplate>
++|<%#DataBinder.Eval(Container.DataItem,"dname")%>|++&nbsp;
<%#DataBinder.Eval(Container.DataItem,"ddate")%>
<br>
<%#DataBinder.Eval(Container.DataItem,"dbody")%>
<br>
=======<%#DataBinder.Eval(Container.DataItem,"reply")%><br>
----------------
<%if(Session["login"]=="yes"){%>
<asp:LinkButton ID="rep" Runat="server" text="reply" Commandname="reply" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id")%>' >
</asp:LinkButton>
<asp:LinkButton ID="del" Runat="server" text="edit" Commandname="edit" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id")%>'>
</asp:LinkButton>
<asp:LinkButton ID="delete" Runat="server" text="delete" Commandname="delete" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id")%>'>
</asp:LinkButton>
<%}%>
<br>
</ItemTemplate>
</asp:repeater>
<!--end repeater--></P>
<P>
<asp:Panel id="panreply" runat="server" Visible="False">
<asp:TextBox id="replybox" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button id="replygo" runat="server" Text="reply ok!"></asp:Button>
<asp:Label id="replyid" runat="server" Visible="False"></asp:Label>
</asp:Panel></P>
<P>
<asp:Panel id="panedit" runat="server" Visible="False">
<asp:TextBox id="editbox" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button id="editgo" runat="server" Text="let's delete it!"></asp:Button>
<asp:Label id="editid" runat="server" Visible="False"></asp:Label>
</asp:Panel></P>
<P>
<asp:Panel id="panadd" runat="server" Visible="False">
<P></P>
<asp:TextBox id="namebox" runat="server"></asp:TextBox>
<P></P>
<P>
<asp:TextBox id="bodybox" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button id="addgo" runat="server" Text="ok,that's all"></asp:Button></P>
<P>
</asp:Panel></P>
</FONT>
</form>
</body>
</HTML>

guestbook.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Text;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace guest
{
/// <summary>
/// guestbook 的摘要说明。
/// </summary>
public class guestbook : System.Web.UI.Page
{
OleDbConnection conn;
OleDbDataAdapter ada;
OleDbCommand cmd;
protected System.Web.UI.WebControls.TextBox adminid;
protected System.Web.UI.WebControls.TextBox adminpd;
protected System.Web.UI.WebControls.Repeater guestbody;
protected System.Web.UI.WebControls.Label ck;
protected System.Web.UI.WebControls.RequiredFieldValidator idreq;
protected System.Web.UI.WebControls.RequiredFieldValidator pdreq;
protected System.Web.UI.WebControls.Panel panadmin;
protected System.Web.UI.WebControls.LinkButton squit;
protected System.Web.UI.WebControls.TextBox replybox;
protected System.Web.UI.WebControls.Button replygo;
protected System.Web.UI.WebControls.Panel panreply;
protected System.Web.UI.WebControls.Label replyid;
protected System.Web.UI.WebControls.TextBox editbox;
protected System.Web.UI.WebControls.Button editgo;
protected System.Web.UI.WebControls.Label editid;
protected System.Web.UI.WebControls.Panel panedit;
protected System.Web.UI.WebControls.LinkButton writego;
protected System.Web.UI.WebControls.Panel panadd;
protected System.Web.UI.WebControls.Button addgo;
protected System.Web.UI.WebControls.TextBox namebox;
protected System.Web.UI.WebControls.TextBox bodybox;
protected System.Web.UI.WebControls.LinkButton logingo;
protected System.Web.UI.WebControls.Button adminenter;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!(Page.IsPostBack))
{
bind();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.adminenter.Click += new System.EventHandler(this.adminenter_Click);
this.squit.Click += new System.EventHandler(this.squit_Click);
this.writego.Click += new System.EventHandler(this.writeid_Click);
this.guestbody.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.guestbody_ItemCommand);
this.replygo.Click += new System.EventHandler(this.replygo_Click);
this.editgo.Click += new System.EventHandler(this.editgo_Click);
this.addgo.Click += new System.EventHandler(this.addgo_Click);
this.logingo.Click += new System.EventHandler(this.logingo_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

//进行数据的绑定
private void bind()
{
StringBuilder connstr=new StringBuilder(ConfigurationSettings.AppSettings["color"]);
conn=new OleDbConnection(connstr.ToString());
StringBuilder sql=new StringBuilder("select * from guestbook order by id desc");
ada=new OleDbDataAdapter(sql.ToString(),conn);
DataSet ds1=new DataSet();
ada.Fill(ds1,0,4,"gbs");
guestbody.DataSource=ds1.Tables["gbs"];
guestbody.DataBind();
}

//这个函数主要来进行登陆验证,用的是以前写asp程序时常用的办法
private void adminenter_Click(object sender, System.EventArgs e)
{
StringBuilder connstr=new StringBuilder(ConfigurationSettings.AppSettings["color"]);
StringBuilder sid=new StringBuilder(adminid.Text);
StringBuilder spd=new StringBuilder(adminpd.Text);
StringBuilder sqladmin=new StringBuilder();
sqladmin.Append("select * from admin");
sqladmin.Append(" where id='");
sqladmin.Append(sid.ToString());
sqladmin.Append("'");
//ck.Text=sqladmin.ToString();
conn=new OleDbConnection(connstr.ToString());
cmd=new OleDbCommand(sqladmin.ToString(),conn);
conn.Open();
OleDbDataReader dr=cmd.ExecuteReader();
//查找sid,看是否有匹配的记录
if(dr.Read())
{
if(dr["pd"].ToString()==spd.ToString())
{
//确定具有管理权限,现在的这个太简单,我还没有深入的研究这个问题
Session["login"]="yes";
panadmin.Visible=false;
squit.Visible=true;
}
}
//如果没有就另外赋值
else
Session["login"]="no";
ck.Text=Session["login"].ToString();
dr.Close();
conn.Close();

}

//退出系统,现在的这个太简单,我还没有深入的研究这个问题
private void squit_Click(object sender, System.EventArgs e)
{
Session["login"]="no";
ck.Text=Session["login"].ToString();
squit.Visible=false;
}

//点击回复或编辑或删除后触发的事件
private void guestbody_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if(((LinkButton)e.CommandSource).CommandName.ToString()=="reply")
{
panreply.Visible=true;
panedit.Visible=false;
replyid.Text=((LinkButton)e.CommandSource).CommandArgument.ToString();
}
if(((LinkButton)e.CommandSource).CommandName.ToString()=="edit")
{
panedit.Visible=true;
panreply.Visible=false;
editid.Text=((LinkButton)e.CommandSource).CommandArgument.ToString();
}
//删除时响应的事件
if(((LinkButton)e.CommandSource).CommandName.ToString()=="delete")
{
StringBuilder connstr=new StringBuilder(ConfigurationSettings.AppSettings["color"]);
conn=new OleDbConnection(connstr.ToString());
StringBuilder deletesql=new StringBuilder();
deletesql.Append("delete * from guestbook where id=");
deletesql.Append(Int32.Parse(((LinkButton)e.CommandSource).CommandArgument));
cmd=new OleDbCommand(deletesql.ToString(),conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
ck.Text=ex.Message.ToString();
}
finally{
conn.Close();
}
bind();
}
}

//回复时响应的事件
private void replygo_Click(object sender, System.EventArgs e)
{
StringBuilder connstr=new StringBuilder(ConfigurationSettings.AppSettings["color"]);
conn=new OleDbConnection(connstr.ToString());
StringBuilder replysql=new StringBuilder();
replysql.Append("update guestbook set reply='");
replysql.Append(replybox.Text.ToString());
replysql.Append("' where id=");
replysql.Append(Int32.Parse(replyid.Text));
cmd=new OleDbCommand(replysql.ToString(),conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
ck.Text=ex.Message.ToString();
}
finally{
conn.Close();
}
panreply.Visible=false;
bind();
}

//编辑时响应的事件
private void editgo_Click(object sender, System.EventArgs e)
{
StringBuilder connstr=new StringBuilder(ConfigurationSettings.AppSettings["color"]);
conn=new OleDbConnection(connstr.ToString());
StringBuilder editsql=new StringBuilder();
editsql.Append("update guestbook set dbody='");
editsql.Append(editbox.Text.ToString());
editsql.Append("' where id=");
editsql.Append(Int32.Parse(editid.Text));
cmd=new OleDbCommand(editsql.ToString(),conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
ck.Text=ex.Message.ToString();
}
finally{
conn.Close();
}
panedit.Visible=false;
bind();

}

//点击留言时响应的事件
private void writeid_Click(object sender, System.EventArgs e)
{
panadd.Visible=true;

}

//添加新留言时响应的事件,在留言表内容机较少的时候可以这样,但是如果很多的话,用这种方法显然会容易发生错误
private void addgo_Click(object sender, System.EventArgs e)
{
StringBuilder connstr=new StringBuilder(ConfigurationSettings.AppSettings["color"]);
conn=new OleDbConnection(connstr.ToString());
StringBuilder addsql=new StringBuilder();
addsql.Append("insert into guestbook (dname,ddate,dbody,reply) values('");
addsql.Append(namebox.Text.ToString());
addsql.Append("','");
addsql.Append(System.DateTime.Now);
addsql.Append("','");
addsql.Append(bodybox.Text.ToString());
addsql.Append("','no");
addsql.Append("')");
cmd=new OleDbCommand(addsql.ToString(),conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
ck.Text=ex.Message.ToString();
}
finally{
conn.Close();
}
panadd.Visible=false;
bind();
}

//进入管理的入口
private void logingo_Click(object sender, System.EventArgs e)
{
panadmin.Visible=true;
}
}
}


你可能感兴趣的:(.net)