动态网站制作指南



当前位置 > 网站建设学院 > 网络编程 > ASP教程 Rss
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket

asp+语法教程(五)asp+的服务器端编程控件篇


发表日期:2000-11-9


使用定制的服务器控件
asp+中提供了45个已经做好了的服务器控件,我们可以将他们象黑盒子一样的使用。除此以外,开发者还可以使用任何第三方开发的服务器控件
在下面的例子中,我们要用到一个通过<acme:calendar runat=server>标签声明的组件,请注意,在文件的第一行必须使用<% Register %> 来声明 "Acme xml "标签的的前缀"Acme".在asp+ 文件中,将会使用这个命名的服务器控件的类的实例。
<%@ Register TagPRefix="Acme" Namespace="Acme" %>

<html>
<head>
<link rel="stylesheet"href="intro.CSS">
</head>

<script language="VB" runat=server>

Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Message.Text = "Hi " & Name.Text & ", you selected: " & Category.SelectedItem.Text & " on: " & MyCal.Date
End Sub

</script>

<body>

<center>

<form action="intro7.aspx" method="post" runat="server">

<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

<h3> Name: <asp:textbox id="Name" runat="server"/>

Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>

<asp:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>

<p>

<Acme:Calendar id="MyCal" runat=server/>

<p>

<asp:label id="Message" runat="server"/>

</form>

</center>

</body>
</html>

声明Acme 的 文件
using System;
using System.Web;
using System.Web.UI;
using System.Collections;

namespace Acme
{
public class Calendar : Control, IPostBackEventHandler, IPostBackDataHandler
{
private String[] monthNames = new String[12];
private DateTime currentDate = DateTime.Now;
private String backColor = "#dcdcdc";
private String foreColor = "#eeeeee";

protected override void Init()
{
// Todo: We should remove the need to call this
Page.RegisterRequiresPostBack(this);
Page.RegisterPostBackScript();

currentDate = DateTime.Now;

// Todo: Hack needed because COOL doesn't support array initializers yet
monthNames[0] = "January";
monthNames[1] = "February";
monthNames[2] = "March";
monthNames[3] = "April";
monthNames[4] = "May";
monthNames[5] = "June";
monthNames[6] = "July";
monthNames[7] = "August";
monthNames[8] = "September";
monthNames[9] = "October";
monthNames[10] = "November";
monthNames[11] = "December";
}

protected override void LoadState(Object viewState)
{
// If we've done a post-back, the old date will be available to us

if (null != viewState)
{
currentDate = DateTime.Parse((String) viewState);
}
}

public void RaisePostBackEvent(String eventArgument)
{
//Page.Response.Write("RaisePostBackEvent Called!!!");

if (eventArgument == null)
{
return;
}

// Keep track of old date (for event firing purposes)

DateTime oldDate = currentDate;

// Todo: We should have post-back take two arguments: eventname and eventarguments

if (String.Compare("NavNextMonth", eventArgument, true) == 0)
{
currentDate = currentDate.AddMonths(1);
}
else if (String.Compare("NavPrevMonth", eventArgument, true) == 0)
{
currentDate = currentDate.AddMonths(-1);
}
else
{
int daySelected = Int32.Parse(eventArgument);
currentDate = new DateTime(currentDate.Year, currentDate.Month, daySelected);
}
}

protected override Object SaveState()
{
// Save CurrentDate out as view state for postback scenarios

return currentDate.ToString();
}

protected override void Render(HtmlTextWriter output)
{
//Response.Write(Page.Request.UserAgent);

if (Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1)
RenderUpLevel(output);
else
RenderDownLevel(output);
}

protected void RenderUpLevel(HtmlTextWriter output)
{
output.WriteLine("<input name='" + UniqueID + "_CurrentDate' id='" + UniqueID + "_CurrentDate' type=hidden>");
output.WriteLine("<span id='" + UniqueID + "'></span>");
output.WriteLine("<script language=jscript>drawcalendar('" + UniqueID + "', '" + Int32.Format(currentDate.Year, null) + "/" + Int32.Format(currentDate.Month, null) + "/" + Int32.Format(currentDate.Day, null) + "');</script>");
}

protected override void PreRender()
{
String DHTMLFunction = "";

DHTMLFunction += "<script language='javaScript'> \n";
DHTMLFunction += " function drawcalendar(calname, newDate) \n";
DHTMLFunction += " { \n";
DHTMLFunction += " var CurrentDate = new Date(newDate);\n";
DHTMLFunction += " var MonthArray = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');\n";
DHTMLFunction += " var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);\n";
DHTMLFunction += " var calText;\n";
DHTMLFunction += " calText = '<table bgcolor=#dcdcdc border=0 height=190 valign=top>';\n";
DHTMLFunction += " calText = calText + '<tr><td>';\n";
DHTMLFunction += " calText = calText + '<center>';\n";
DHTMLFunction += " calText = calText + \"<a href='Javascript:drawcalendar(\\\"\" + calname + \"\\\", \\\"\" + CurrentDate.getFullYear() + \"/\" + CurrentDate.getMonth() + \"/\" + CurrentDate.getDate() + \"\\\")'>\";\n";
DHTMLFunction += " calText = calText + '<img src=/quickstart/aspplus/images/left4.gif width=11 height=11 border=0></a>';\n";
DHTMLFunction += " calText = calText + ' <b>' + MonthArray[CurrentDate.getMonth()] + ' ' + CurrentDate.getFullYear() + '</b>';\n";
DHTMLFunction += " calText = calText + \" <a href='javascript:drawcalendar(\\\"\" + calname + \"\\\", \\\"\" + CurrentDate.getFullYear() + \"/\" + (CurrentDate.getMonth() + 2) + \"/\" + CurrentDate.getDate() + \"\\\")'>\";\n";
DHTMLFunction += " calText = calText + '<IMG SRC=/quickstart/aspplus/images/right4.gif width=11 height=11 border=0></a>';\n";
DHTMLFunction += " calText = calText + '</center>';\n";
DHTMLFunction += " calText = calText + '</td></tr>';\n";
DHTMLFunction += " calText = calText + '<tr valign=top><td valign=top>';\n";
DHTMLFunction += " calText = calText + '<table border=1 bgcolor=#eeeeee height=160>';\n";
DHTMLFunction += " calText = calText + '<tr height=20>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Sun </td>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Mon </td>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Tue </td>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Wed </td>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Thu </td>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Fri </td>';\n";
DHTMLFunction += " calText = calText + ' <td align=right width=23> Sat </td>';\n";
DHTMLFunction += " calText = calText + '</tr>';\n";
DHTMLFunction += " calText = calText + '<tr>';\n";
DHTMLFunction += " var numDays = MonthDays[CurrentDate.getMonth()];\n";
DHTMLFunction += " var firstDay = new Date(1999, 8, 1).getDay();\n";
DHTMLFunction += " for (var x=0; x<firstDay; x++)\n";
DHTMLFunction += " {\n";
DHTMLFunction += " calText = calText + '<td align=right width=23></td>'\n";
DHTMLFunction += " }\n";
DHTMLFunction += " for (var x=1; x<=numDays; x++) \n";
DHTMLFunction += " { \n";
DHTMLFunction += " if (CurrentDate.getDate() == x) \n";
DHTMLFunction += " { \n";
DHTMLFunction += " calText = calText + '<td align=right width=23>';\n";
DHTMLFunction += " calText = calText + '<font color=red><b><u>' + x + '</u></b></font>';\n";
DHTMLFunction += " calText = calText + '</td>';\n";
DHTMLFunction += " }\n";
DHTMLFunction += " else \n";
DHTMLFunction += " { \n";
DHTMLFunction += " calText = calText + '<td align=right width=23>';\n";
DHTMLFunction += " calText = calText + \"<a href='javascript:drawcalendar(\\\"\" + calname + \"\\\", \\\"\" + CurrentDate.getFullYear() + \"/\" + (CurrentDate.getMonth()+1) + \"/\" + x + \"\\\")'>\" + x + \"</a>\";";
DHTMLFunction += " calText = calText + '</td>';\n";
DHTMLFunction += " }\n";
DHTMLFunction += " if (((firstDay+x) % 7) == 0)\n";
DHTMLFunction += " {\n";
DHTMLFunction += " calText = calText + '</tr><tr>';\n";
DHTMLFunction += " }\n";
DHTMLFunction += " }\n";
DHTMLFunction += " calText = calText + '</tr>';";
DHTMLFunction += " calText = calText + '</table></td></tr></table>';";
DHTMLFunction += " var CalendarSpan = document.all(calname);";
DHTMLFunction += " if (CalendarSpan != null)";
DHTMLFunction += " CalendarSpan.innerHTML = calText;";
DHTMLFunction += " var CalendarValue = document.all(calname + '_CurrentDate');";
DHTMLFunction += " if (CalendarValue != null)";
DHTMLFunction += " CalendarValue.value = '' + (CurrentDate.getMonth() + 1) + '/' + CurrentDate.getDate() + '/' + CurrentDate.getFullYear();";
DHTMLFunction += " } \n";
DHTMLFunction += "</script>\n";

if (Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1)
Page.RegisterClientScriptBlock("ACME_CALENDAR_DHTML", DHTMLFunction);
}

protected void RenderDownLevel(HtmlTextWriter output)
{
// Output Calendar Header

output.WriteLine("<table bgcolor=" + backColor + " border=0 height=190 valign=top><tr><td>");
output.WriteLine("<table bgcolor=" + backColor + " border=0 height=190 valign=top>");
output.WriteLine("<tr><td>");
output.WriteLine("<center>");
output.WriteLine(" <a href=\"javascript:" + Page.GetPostBackEventReference(this, "NavPrevMonth") + "\">");
output.WriteLine("<img src=/quickstart/aspplus/images/left4.gif width=11 height=11 border=0></a>");
output.WriteLine(" <b>" + monthNames[currentDate.Month-1] + " " + Int32.Format(currentDate.Year, null) + "</b>");
output.WriteLine(" <a href=\"javascript:" + Page.GetPostBackEventReference(this, "NavNextMonth") + "\">");
output.WriteLine("<IMG SRC=/quickstart/aspplus/images/right4.gif width=11 height=11 border=0></a>");
output.WriteLine("</center>");
output.WriteLine("</td></tr>");
output.WriteLine("<tr valign=top><td valign=top>");
output.WriteLine("<table border=1 bgcolor=" + foreColor + " height=160>");
output.WriteLine("<tr height=20>");
output.WriteLine(" <td align=right width=23> Sun </td>");
output.WriteLine(" <td align=right width=23> Mon </td>");
output.WriteLine(" <td align=right width=23> Tue </td>");
output.WriteLine(" <td align=right width=23> Wed </td>");
output.WriteLine(" <td align=right width=23> Thu </td>");
output.WriteLine(" <td align=right width=23> Fri </td>");
output.WriteLine(" <td align=right width=23> Sat </td>");
output.WriteLine("</tr>");
output.WriteLine("<tr>");

// Calculate how many days are in the month

int numDays = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);

// Calculate what day of week the first day of the month is on

int firstDay = new DateTime(currentDate.Year, currentDate.Month, 1).DayOfWeek;

// Pre-Day Padding

for (int x=0; x<firstDay; x++)
{
output.WriteLine("<td align=right width=23></td>");
}

// Output each day

for (int x=1; x<=numDays; x++)
{
if (currentDate.Day == x)
{
output.Write("<td align=right width=23>");
output.Write("<font color=red><b><u>" + Int32.Format(x, null) + "</u></b></font>");
output.WriteLine("</td>");
}
else
{
output.Write("<td align=right width=23>");
output.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this, Int32.Format(x, null)) + "\">");
output.Write(Int32.Format(x, null) + "</a>");
output.WriteLine("</td>");
}

// PerPage row break as appropriate
if (((firstDay+x) % 7) == 0)
{
output.WriteLine("</tr><tr>");
}
}

output.WriteLine("</tr>");
output.WriteLine("</table></td></tr></table></table>");
}

public DateTime Date
{
get
{
return currentDate;
}
set
{
currentDate = value;
}
}

public String BackColor
{
get
{
return backColor;
}
set
{
backColor = value;
}
}

public String ForeColor
{
get
{
return foreColor;
}
set
{
foreColor = value;
}
}

// Todo: We should eliminate the need for a control developer to do stub
// implementations of the below standard IPostDataHandler Methods

public bool LoadPostData(String postDataKey, NameValueCollection values)
{
String clientDate = values[UniqueID + "_CurrentDate"];

if (clientDate != null)
currentDate = DateTime.Parse(clientDate);

return false;
}

public void RaisePostDataChangedEvent()
{

}
}
}

生成服务器控件的文件

csc /t:library /r:System.Web.dll /r:System.dll /out:..\..\..\bin\Acme.dll Acme.cs
csc /t:library /out:..\..\..\bin\WebMarket.dll WebMarket.cs

这个例子的演示在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro7.aspx
这个日历的服务器控件可以同时支持高级和低级的浏览器,对于高级的浏览器,他在客户端生成DHTML 代码,DHTML 文档不需要和服务器进行交互!对于低级的浏览器,则生成标准的 HTML 3.2 的代码,这个时候需要和服务器的交互来处理浏览器客户端的用户操作
注意:这个页面的代码的开发者在书写的过程中,开发者不需要关心客户浏览器是高级的还是低级的。日历控件自己会处理两种浏览器之间的差别
下一讲,我们将要进入asp+ 操作 数据库 的章节,请大家随时留意我们的站点更新!

关注此文的读者还看过:
·2012-5-23 8:12:36 asp学习入门经验谈
·2012-5-23 8:12:06 @ 指令参考之二 - @ENABLESESSIONSTATE
·2012-5-23 8:12:06 WSH实用讲座:第一讲 获取机器的网络属性配置
·2012-5-23 8:11:24 ASP设计常见问题及解答精要-4
·2012-5-23 8:11:11 Asp深度揭密(上)
·2012-5-23 8:10:47 asp+语法介绍(一)
·2012-5-23 8:09:20 NET移植案例学习:建造Web站点(7)
·2012-5-23 8:07:27 亲密接触ASP.Net(16) 
·2012-5-23 8:07:20 亲密接触ASP.Net(8)
站长推荐 PS笔刷下载 在线翻译 系统进程 广告代码
  发表评论
姓 名: 验证码:
内 容:
教程搜索服务
ASP源码推荐
·产品订购模块 v1.0
·助跑Flash生成系统
·爱墙ACCSESS版带查询和后台
·齐齐哈尔职业学院计算机软件052
·Lirong 网络办公系统 2006 企业
·2XL 留言本 v2.3 GB2312
·中国英才招聘网程序
·风讯网站内容管理系统(FoosunCM
·sogo友情链接程序 v1.0
·动力先锋仿阿里巴巴B2B电子商务
·奥搜垂直搜索引擎旅游版 build
·网趣网上购物系统时尚版 v11.1
项目外包信息
·全球锁医疗科技有限公司LOGO设计
·多个.NET网站制作和修改
·总计400元长期外包企业官网内页
·淘宝旗舰店装修 5000~10000元
·厦门巨游网络科技有限公司
·影派平板电脑
·寻会php的程序员外包网站
·派桑网络-网络营销专家
·汽车配件网站制作 50000元
·整站SEO优化
·课件门户网程序
·求长期合作网站设计制作高手
·公司网站重新改版 8000元
·asp企业网站小改动
·网站flash片头
发布信息 浏览信息
邮件订阅服务
输入你的邮件地址,你将不会错过任何关于<ASP教程>的内容


网络编程文章分类
ASP教程
ASP实例
ASP技巧
ASP文摘
PHP教程
PHP技巧
PHP实例
PHP文摘
JSP教程
JSP技巧
JSP实例
JSP文摘
ASP.NET教程
ASP.NET技巧
ASP.NET实例
ASP.NET应用
xml教程
xsl教程
xml技巧
C#教程
C#应用
Delphi教程
Perl教程
Shell教程
Ajax教程
Visual Basic教程
Java教程
J2EE/J2ME教程
C/C++教程
移动解决方案
移动短信技术
移动行业动态
软件工程
WordPress
Android开发
站长工具:Google PR查询|Alexa排名查询|网站速度测试|CSS在线编辑器|OPEN参数生成器|弹出式窗口代码产生器|密码登录生成器|在线按钮生成器|Meta标签生成器|邮箱图标在线生成|多色彩特效字代码生成器|网页代码调试器|在线FTP登陆|Flash取色器|配色代码对照表|配色辞典|CSS生成器|CSS在线压缩|广告代码|框架网页代码生成器|js/vbs加密|md5加密|进制转换|UTF-8 转换工具|在线调色板|Html转换js|Html转换asp|Html转换php|Html转换perl
实用工具:汉字翻译拼音|拼音字典|在线翻译|天气预报|火星文|在线网速测试|符号对照表|个税计算|理财工具|黄金价格|购房银行按揭利率计算|汇率查询|经典小工具|汉字简繁转换|普通单位换算|公制单位换算|生辰老黄历|国内电话区号|国家代码与域名缩写|文字加密解密|元素周期表|健康查询|世界时间|全国各地车牌查询|全国车辆交通违章查询|万年历|二十四节气|汉字横竖排版|手机号码查询|计算器|ip搜索|酒店预订|机票预订
广告刊登 | 版权声明 | 联系我们 | 加入收藏 | RSS订阅
Copyright © 2000-2012 www.knowsky.com All rights reserved | 沪ICP备05001343号