使用纯粹的asp+语言制作的栏目管理(一)

http://www.asp888.net 豆腐技术站

昨天的asx 版本的栏目管理和以前的 留言版的程序自从推出以后,反响不错,但是很多网友纷纷提出了新的问题,他们认为 这两个程序其实只是 asp 文件简单的升级到aspx 文件,大家并没有从这些程序中看出aspx的新的特征,纷纷要求 豆腐 使用aspx 的特性来制作一个 aspx 版本的程序,还有的 朋友要求 编程的语言不要再 使用 VB,而是使用C# 语句,其实 MS 推荐的语言是 VB,不过为了 照顾大家学习新知识的渴望,豆腐 又 推出了这个以 纯粹的 aspx 特性+C# 语言制作的 栏目管理程序,下载会在 很快制作完毕。

现在首先看看 这个新的 add.aspx

1@ Assembly Name="System.Net" 
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.SQL" 
 1<script language="C#" runat="server">   
 2protected void Page_Load(Object Src, EventArgs E){   
 3SQLDataReader dbRead;   
 4SQLCommand dbComm;   
 5String strSQL;   
 6String strConn;   
 7SQLConnection conn;   
 8Hashtable Cfg=new Hashtable();   
 9Cfg = (Hashtable)Context.GetConfig("appsettings");   
10strConn=Cfg["Conn"].ToString();   
11conn = new SQLConnection(strConn);   
12strSQL="select * from lanmuclass order by classid";   
13dbComm = new SQLCommand(strSQL, conn);   
14dbComm.ActiveConnection.Open();   
15dbComm.Execute(out dbRead);   
16while(dbRead.Read()){   
17//这个程序是 在 DropDownList 的 显示和Value 不一致的时候使用   
18ListItem li = new ListItem();   
19li.Text = dbRead["classname"].ToString();   
20li.Value = dbRead["classid"].ToString();   
21selClass.Items.Add(li);   
22}   
23//如果 显示 和 Value 一直的话,则简单的这样就可以了 
24
25selFrom.Items.Add("原创");   
26selFrom.Items.Add("转载");   
27selFrom.Items.Add("翻译");   
28selFrom.Items.Add("资料整理"); 
29
30//如果不在<asp:TextBox 中设置 TextMode 属性,也可以这样设置   
31//txtPass.TextMode = TextBoxMode.Password;   
32}   
33</script>
 1<html>
 2<head>
 3<title>增加文章</title>
 4<link href="/doufu.css" rel="stylesheet" type="text/css"/>
 5</head>
 6<body>
 7<form action="doSaveAdd.aspx" method="post">
 8<asp:table cellpadding="15" cellspacing="0" font-name="Verdana" font-size="8pt" gridlines="Both" horizontalalign="Center" id="tableTest" runat="server" width="100%">
 9<asp:tablerow runat="server">
10<asp:tablecell width="20%">呢称</asp:tablecell>
11<asp:tablecell width="30%"><asp:textbox id="txtName" runat="server"></asp:textbox></asp:tablecell>
12<asp:tablecell width="20%">密码</asp:tablecell>
13<asp:tablecell width="30%"><asp:textbox id="txtPass" runat="server" textmode="Password"></asp:textbox></asp:tablecell>
14</asp:tablerow>
15<asp:tablerow runat="server">
16<asp:tablecell width="20%">文章类别</asp:tablecell>
17<asp:tablecell colspan="3" width="30%"><asp:dropdownlist id="selClass" runat="server"></asp:dropdownlist></asp:tablecell>
18</asp:tablerow>
19<asp:tablerow runat="server">
20<asp:tablecell width="20%">发表类别</asp:tablecell>
21<asp:tablecell colspan="3" width="30%"><asp:dropdownlist id="selFrom" runat="server"></asp:dropdownlist></asp:tablecell>
22</asp:tablerow>
23<asp:tablerow runat="server">
24<asp:tablecell width="20%">文章标题</asp:tablecell>
25<asp:tablecell colspan="3" width="30%">
26<asp:textbox id="txtTitle" runat="server"></asp:textbox>
27<asp:button id="cmdDo" runat="server" text="确定增加"></asp:button>
28</asp:tablecell>
29</asp:tablerow>
30<asp:tablerow runat="server">
31<asp:tablecell width="20%">文章内容</asp:tablecell>
32<asp:tablecell colspan="3" width="30%"><asp:textbox cols="40" id="txtContent" rows="20" runat="server" textmode="MultiLine"></asp:textbox></asp:tablecell>
33</asp:tablerow>
34</asp:table>
35</form>
36</body>
37</html>

这里的这个程序很简单,但是他用到了 aspx 的一些特殊的属性,同时 由于 C# 是 区分大小写的 语言,所以大家在 从 VB 转到 C# 的时候 一定要非常的小心。
doSaveAdd.aspx文件的内容:

1@ Assembly Name="System.Net" 
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.SQL" 
 1<script language="C#" runat="server">   
 2protected void Page_Load(Object Src, EventArgs E){   
 3String strConn;   
 4SQLConnection conn;   
 5Hashtable Cfg=new Hashtable();   
 6Cfg = (Hashtable)Context.GetConfig("appsettings");   
 7strConn=Cfg["Conn"].ToString();   
 8conn = new SQLConnection(strConn);   
 9String strName=Request.Form["txtName"].ToString();   
10String strPass=Request.Form["txtPass"].ToString();   
11if(strName==""){   
12showmsg.Text="对不起,用户名称是 闭填项目";   
13return;   
14}   
15String strSQL;   
16//首先校验用户和密码是否正确   
17SQLDataReader dbRead;   
18strSQL="select UserPassword from bbsuser where username='" + strName + "'";   
19SQLCommand sqlCmd=new SQLCommand(strSQL,conn);   
20sqlCmd.ActiveConnection.Open();   
21sqlCmd.Execute(out dbRead);   
22if(!dbRead.Read()){   
23showmsg.Text="对不起,这个用户不存在!";   
24return;   
25}   
26if(dbRead["UserPassword"].ToString()!=strPass){   
27showmsg.Text="对不起,用户名称和用户密码不匹配!";   
28return;   
29}   
30sqlCmd.ActiveConnection.Close();   
31//密码匹配,将用户输入的文本信息保存到数据库中   
32//因为是 演示 程序,所以就没有 检验 标题和内容 的合法性   
33String strClassId=Request.Form["selClass"];   
34String strSelFrom=Request.Form["selFrom"];   
35String strTitle=Request.Form["txtTitle"];   
36String strContent=Request.Form["txtContent"]; 
37
38strSQL="insert into lanmu(classid,title,content,dtime,userid,IsUse,viewnum,selFrom)values(";   
39strSQL=strSQL + "" + strClassId + ",'" + strTitle + "','" + strContent + "',";   
40strSQL=strSQL + "getdate(),'" + strName + "','0',0,'" + strSelFrom + "')";   
41sqlCmd =new SQLCommand(strSQL,conn);   
42sqlCmd.ActiveConnection.Open();   
43sqlCmd.ExecuteNonQuery();   
44//虽然系统可以自动关闭这个Command对象,但是最好还是自己关闭一下   
45sqlCmd.ActiveConnection.Close();   
46}   
47</script>
1<html>
2<head>
3<title>增加文章</title>
4<link href="/doufu.css" rel="stylesheet" type="text/css"/>
5</head>
6<body>
7<asp:label id="showmsg" runat="server" text="恭喜,恭喜,您的文章已经添加到了数据库中!"></asp:label>
8</body>
9</html>

其实,这个 doSaveAdd.aspx 文件其实是 可以不用的,我们只要在 add.aspx 的 cmdDo 上处理他的OnClick 事件就可以了,程序是 一样的,给大家一种新的 选择,不是更好吗?

Published At
Categories with Web编程
Tagged with
comments powered by Disqus