2004-4-9\10+ 用repeater写的留言本

总算是把这个东东写完了。做这个的目的就是为了系统的练习一下最近学的一些知识。并且在一开始就定了一些规则,比如可以用datareader就不用dataset,留言本的主体用repeater。下面把遇到的问题说一下。
首先最大的问题就是确定留言编号的问题,这个编号主要是用来进行回复或删除的时候用来确定具体的记录。可能是有些repeater的属性我还没有学到吧,我实在是想不出来到底该怎么做才可以在点击每条留言都可以顺便返回一个记录编号的办法。也想到过commandname等,可以这个也没有办法啊。最后只好加了一个textbox,手动添加编号进去。如果哪位大侠知道方法的话,请一定要告诉我啊。
接下来的问题就是因为第一个问题引起的,我把delete、reply和add这三个按纽都放到了一起,结果出现的麻烦就是不可以随便的换功能,因为每一个都是submit,一换的话就引发验证控件工作,然后就没有办法转换过去了。
还有就是各个控件的visible属性的控制,也着实让我头痛了好一阵子……
就说这么多。刚才火箭绝地大反击,得分超过掘金啦~~~~

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ import namespace="System.Data"%>
<%@ import namespace="System.Data.OleDb"%>
<script language="c#" runat="server">
string connstr=ConfigurationSettings.AppSettings["color"];
OleDbConnection conn;
//页面初始化
private void Page_Load(object sender,System.EventArgs e){
if(!Page.IsPostBack){
showguest();
if(Session["login"]=="yes"){
pen4.Visible=false;
del.Visible=true;
rep.Visible=true;
quit.Visible=true;
}
else{
del.Visible=false;
rep.Visible=false;
quit.Visible=false;
}
}
}

//给rep1绑定数据
private void showguest(){
conn=new OleDbConnection(connstr);
string sql="select top 4 * from guestbook order by id desc";
OleDbCommand cmd=new OleDbCommand(sql,conn);
conn.Open();
rep1.DataSource=cmd.ExecuteReader();
rep1.DataBind();
conn.Close();
}

//点击留言按纽后响应的事件
private void bt2_click(object source,System.EventArgs e){
rep1.Visible=false;
pan1.Visible=true;
pan2.Visible=false;
button2.Visible=false;
}

//回复时响应的事件
private void bt4_click(object source,System.EventArgs e){
if(Page.IsValid){
conn=new OleDbConnection(connstr);
string sql="select * from guestbook";
OleDbDataAdapter ada=new OleDbDataAdapter(sql,conn);
OleDbCommandBuilder cb=new OleDbCommandBuilder(ada);
DataSet ds=new DataSet();
ada.Fill(ds,"gbook");
DataTable dt4=ds.Tables["gbook"];
dt4.PrimaryKey=new DataColumn[]{dt4.Columns["id"]};
int ids=Int32.Parse(tbs5.Text);
DataRow dr=dt4.Rows.Find(ids);
dr["reply"]=tbs6.Text;
ada.Update(ds,"gbook");
pan2.Visible=false;
rep1.Visible=true;
showguest();
}

//添加新留言时响应的事件
}
private void bt1_click(object source,System.EventArgs e){
if(Page.IsValid){
conn=new OleDbConnection(connstr);
string sql="select * from guestbook order by ddate desc";
OleDbDataAdapter ada=new OleDbDataAdapter(sql,conn);
OleDbCommandBuilder cb=new OleDbCommandBuilder(ada);
DataSet ds=new DataSet();
ada.Fill(ds,"gbook");
DataTable dt1=ds.Tables["gbook"];
DataRow dr1=dt1.NewRow();
dr1["dname"]=tbs1.Text;
dr1["homepage"]=tbs2.Text;
dr1["dbody"]=tbs4.Text;
dr1["ddate"]=System.DateTime.Now;
dt1.Rows.Add(dr1);
ada.Update(ds,"gbook");
pan1.Visible=false;
pan2.Visible=false;
rep1.Visible=true;
button2.Visible=true;
showguest();

}
}

//点击回复时响应的事件
private void bt3_click(object source,System.EventArgs e){
pan2.Visible=true;
//rep1.Vislble=false;
pan1.Visible=false;
}
private void bt5_click(object source,System.EventArgs e){
pan3.Visible=true;
}

//删除留言
private void bt6_click(object source,System.EventArgs e){
if(Page.IsValid){
conn=new OleDbConnection(connstr);
string sql="delete * from guestbook where id="+tbs7.Text;
OleDbCommand cmd=new OleDbCommand(sql,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
pan3.Visible=false;
showguest();
}
}

//点击进入后的事件
private void bt7_click(object source,System.EventArgs e){
pen4.Visible=true;
}

//登陆处理
private void bt8_click(object source,System.EventArgs e){
if(Page.IsValid){
conn=new OleDbConnection(connstr);
string sql="select * from duser where dname='"+tbs8.Text+"'";
OleDbCommand cmd=new OleDbCommand(sql,conn);
conn.Open();
OleDbDataReader dr=cmd.ExecuteReader();
if(dr.Read()){
if(dr["dpd"].ToString()==tbs9.Text){
Session["login"]="yes";
lb1.Text=Session["login"].ToString();
pen4.Visible=false;
del.Visible=true;
rep.Visible=true;
quit.Visible=true;
}
else{
lb1.Text="not well";
del.Visible=false;
rep.Visible=false;
quit.Visible=false;
}
}
}
}

//退出系统
private void bt9_click(object source,System.EventArgs e){
Session["login"]="no";
del.Visible=false;
rep.Visible=false;
quit.Visible=false;
lb1.Text="";
}
</script>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>留言本实验</title>
</head>
<style>
td { font-size: 12px; line-height: 20px; color:#6E0808;font-family:"宋体";}
</style>
<body>
<form runat="server">
<asp:Label id="lb1" runat="server"/>
<asp:panel id="pen4" runat="server" Visible="false">
id:<asp:TextBox id="tbs8" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate="tbs8" runat="server" id="r8" ErrorMessage="error!"/><br>
pd:<asp:TextBox id="tbs9" runat="server" textmode="Password"/>
<asp:RequiredFieldValidator ControlToValidate="tbs9" runat="server" id="r9" ErrorMessage="error!"/><br>
<asp:linkButton id="button8" runat="server" OnClick="bt8_click" Text="gooooo"/>
</asp:panel>
<asp:linkButton id="button7" runat="server" OnClick="bt7_click" Text=" login"/>
<asp:linkButton id="button2" runat="server" OnClick="bt2_click" text="write"/>
<asp:linkButton runat="server" OnClick="bt5_click" text="del" Visible="false" id="del"/>
<asp:linkButton runat="server" OnClick="bt3_click" text="reply" Visible="false" id="rep"/>
<asp:linkButton runat="server" OnClick="bt9_click" text="quit" Visible="false" id="quit"/>
<asp:Repeater id="rep1" runat="server">
<headertemplate>
<table width="200" border="0" cellpadding="0" cellspacing="0">
</headertemplate>
<itemtemplate>
<tr>
<td height="20" colspan="2" valign="top" bgcolor="#00CCFF"><%# DataBinder.Eval(Container,"DataItem.dname")%><br><%# DataBinder.Eval(Container,"DataItem.id")%></td>
<td width="30" valign="top" bgcolor="#CC6633"><a href="<%# DataBinder.Eval(Container,"DataItem.homepage")%>" target="_blank">hp</a></td>
<td width="90" valign="top" bgcolor="#CC6633"><%# DataBinder.Eval(Container,"DataItem.ddate")%></td>
</tr>
<tr>
<td height="20" colspan="4" valign="top" bgcolor="#CCFFCC"><%# DataBinder.Eval(Container,"DataItem.dbody")%></td>
</tr>
<tr>
<td width="30" height="20" valign="top" bgcolor="#999999">reply
</td>
<td colspan="3" valign="top" bgcolor="#D4D0C8"><%# DataBinder.Eval(Container,"DataItem.reply")%></td>
</tr>
<tr>
<td height="1"></td>
<td width="30"></td>
<td></td>
<td></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:Repeater>

<asp:Panel Visible="false" runat="server" id="pan1">
id*:<asp:TextBox id="tbs1" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate="tbs1" runat="server" id="r1" ErrorMessage="error!"/><br>
homepage*:<asp:TextBox id="tbs2" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate="tbs2" runat="server" id="r2" ErrorMessage="error!"/><br>
qq:<asp:TextBox id="tbs3" runat="server"/><br>
body*:<asp:TextBox id="tbs4" TextMode="MultiLine" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate="tbs4" runat="server" id="r4" ErrorMessage="error!"/><br>
<asp:Button id="button1" OnClick="bt1_click" text="ok let us go" runat="server"/><br>
</asp:Panel>

<asp:panel id="pan2" Visible="false" runat="server">
id*:<asp:TextBox id="tbs5" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate="tbs5" runat="server" id="r5" ErrorMessage="error!"/><br>
rebody:<asp:TextBox id="tbs6" TextMode="MultiLine" runat="server"/>
<asp:Button id="button4" OnClick="bt4_click" text="ok let us go" runat="server"/><br>
</asp:panel>
<asp:panel id="pan3" Visible="false" runat="server">
id*:<asp:TextBox id="tbs7" runat="server"/>
<asp:RequiredFieldValidator ControlToValidate="tbs7" runat="server" id="r7" ErrorMessage="error!"/><br>
<asp:Button id="button6" OnClick="bt6_click" text="delete" runat="server"/><br>
</asp:panel>
</form>

</body>
</html>

posted on 2006-06-26 13:38  Notus|南色的风  阅读(532)  评论(13编辑  收藏  举报