昨天的asx 版本的欄目管理和以前的 留言版的程序自從推出以后,反響不錯,但是很多網友紛紛提出了新的問題,他們認為 這兩個程序其實只是 asp 文件簡單的升級到aspx 文件,大家并沒有從這些程序中看出aspx的新的特征,紛紛要求 豆腐 使用aspx 的特性來制作一個 aspx 版本的程序,還有的 朋友要求 編程的語言不要再 使用 VB,而是使用C# 語句,其實 MS 推薦的語言是 VB,不過為了 照顧大家學習新知識的渴望,豆腐 又 推出了這個以 純粹的 aspx 特性+C# 語言制作的 欄目管理程序,下載會在 很快制作完畢。
現在首先看看 這個新的 add.aspx
<%@ Assembly Name="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<script language="C#" runat=server>
protected void Page_Load(Object Src, EventArgs E){
SQLDataReader dbRead;
SQLCommand dbComm;
String strSQL;
String strConn;
SQLConnection conn;
Hashtable Cfg=new Hashtable();
Cfg = (Hashtable)Context.GetConfig("appsettings");
strConn=Cfg["Conn"].ToString();
conn = new SQLConnection(strConn);
strSQL="select * from lanmuclass order by classid";
dbComm = new SQLCommand(strSQL, conn);
dbComm.ActiveConnection.Open();
dbComm.Execute(out dbRead);
while(dbRead.Read()){
//這個程序是 在 DropDownList 的 顯示和Value 不一致的時候使用
ListItem li = new ListItem();
li.Text = dbRead["classname"].ToString();
li.Value = dbRead["classid"].ToString();
selClass.Items.Add(li);
}
//如果 顯示 和 Value 一直的話,則簡單的這樣就可以了
selFrom.Items.Add("原創");
selFrom.Items.Add("轉載");
selFrom.Items.Add("翻譯");
selFrom.Items.Add("資料整理");
//如果不在<asp:TextBox 中設置 TextMode 屬性,也可以這樣設置
//txtPass.TextMode = TextBoxMode.Password;
}
</script>
<html>
<head>
<title>增加文章</title>
<link rel="stylesheet" type="text/css" href="/doufu.css">
</head>
<body>
<form action="doSaveAdd.aspx" method=post>
<asp:Table id="tableTest" width=100% GridLines="Both" Runat="server" HorizontalAlign="Center" Font-Name="Verdana" Font-Size="8pt" CellPadding=15 CellSpacing=0>
<asp:TableRow runat=server>
<asp:TableCell width=20%>呢稱</asp:TableCell>
<asp:TableCell width=30%><asp:TextBox id="txtName" runat=server /></asp:TableCell>
<asp:TableCell width=20%>密碼</asp:TableCell>
<asp:TableCell width=30%><asp:TextBox id="txtPass" TextMode = Password runat=server /></asp:TableCell>
</asp:TableRow>
<asp:TableRow runat=server>
<asp:TableCell width=20%>文章類別</asp:TableCell>
<asp:TableCell width=30% colspan=3><asp:DropDownList id=selClass runat=server /></asp:TableCell>
</asp:TableRow>
<asp:TableRow runat=server>
<asp:TableCell width=20%>發表類別</asp:TableCell>
<asp:TableCell width=30% colspan=3><asp:DropDownList id=selFrom runat=server /></asp:TableCell>
</asp:TableRow>
<asp:TableRow runat=server>
<asp:TableCell width=20%>文章標題</asp:TableCell>
<asp:TableCell width=30% colspan=3>
<asp:TextBox id="txtTitle" runat=server />
<asp:Button id="cmdDo" runat=server text="確定增加" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat=server>
<asp:TableCell width=20%>文章內容</asp:TableCell>
<asp:TableCell width=30% colspan=3><asp:TextBox id="txtContent" TextMode=MultiLine rows=20 cols=40 runat=server /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>
</html>
這里的這個程序很簡單,但是他用到了 aspx 的一些特殊的屬性,同時 由于 C# 是 區分大小寫的 語言,所以大家在 從 VB 轉到 C# 的時候 一定要非常的小心。
doSaveAdd.aspx文件的內容:
<%@ Assembly Name="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<script language="C#" runat=server>
protected void Page_Load(Object Src, EventArgs E){
String strConn;
SQLConnection conn;
Hashtable Cfg=new Hashtable();
Cfg = (Hashtable)Context.GetConfig("appsettings");
strConn=Cfg["Conn"].ToString();
conn = new SQLConnection(strConn);
String strName=Request.Form["txtName"].ToString();
String strPass=Request.Form["txtPass"].ToString();
if(strName==""){
showmsg.Text="對不起,用戶名稱是 閉填項目";
return;
}
String strSQL;
//首先校驗用戶和密碼是否正確
SQLDataReader dbRead;
strSQL="select UserPassword from bbsuser where username='" + strName + "'";
SQLCommand sqlCmd=new SQLCommand(strSQL,conn);
sqlCmd.ActiveConnection.Open();
sqlCmd.Execute(out dbRead);
if(!dbRead.Read()){
showmsg.Text="對不起,這個用戶不存在!";
return;
}
if(dbRead["UserPassword"].ToString()!=strPass){
showmsg.Text="對不起,用戶名稱和用戶密碼不匹配!";
return;
}
sqlCmd.ActiveConnection.Close();
//密碼匹配,將用戶輸入的文本信息保存到數據庫中
//因為是 演示 程序,所以就沒有 檢驗 標題和內容 的合法性
String strClassId=Request.Form["selClass"];
String strSelFrom=Request.Form["selFrom"];
String strTitle=Request.Form["txtTitle"];
String strContent=Request.Form["txtContent"];
strSQL="insert into lanmu(classid,title,content,dtime,userid,IsUse,viewnum,selFrom)values(";
strSQL=strSQL + "" + strClassId + ",'" + strTitle + "','" + strContent + "',";
strSQL=strSQL + "getdate(),'" + strName + "','0',0,'" + strSelFrom + "')";
sqlCmd =new SQLCommand(strSQL,conn);
sqlCmd.ActiveConnection.Open();
sqlCmd.ExecuteNonQuery();
//雖然系統可以自動關閉這個Command對象,但是最好還是自己關閉一下
sqlCmd.ActiveConnection.Close();
}
</script>
<html>
<head>
<title>增加文章</title>
<link rel="stylesheet" type="text/css" href="/doufu.css">
</head>
<body>
<asp:Label id=showmsg Text="恭喜,恭喜,您的文章已經添加到了數據庫中!" runat=server />
</body>
</html>
其實,這個 doSaveAdd.aspx 文件其實是 可以不用的,我們只要在 add.aspx 的 cmdDo 上處理他的OnClick 事件就可以了,程序是 一樣的,給大家一種新的 選擇,不是更好嗎?