·建站首页 ·钻石 ·繁體
您的位置: 中国建站之家 -> 网站开发设计 -> .Net教程 -> asp+语法教程(五)asp+的服务器端编程控件篇

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

作者:未知  来源:转载  发布时间:2005-7-20 9:55:21  发布人:acx

使用定制的服务器控件
在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>

<&#115cript 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

</&#115cript>

<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" 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.RegisterPostBack&#115cript(); <br/><br/>currentDate = DateTime.Now; <br/><br/>// Todo: Hack needed because COOL doesn't support array initializers yet <br/>monthNames[0] = "January"; <br/>monthNames[1] = "February"; <br/>monthNames[2] = "March"; <br/>monthNames[3] = "April"; <br/>monthNames[4] = "May"; <br/>monthNames[5] = "June"; <br/>monthNames[6] = "July"; <br/>monthNames[7] = "August"; <br/>monthNames[8] = "September"; <br/>monthNames[9] = "October"; <br/>monthNames[10] = "November"; <br/>monthNames[11] = "December"; <br/>} <br/><br/>protected override void LoadState(Object viewState) <br/>{ <br/>// If we've done a post-back, the old date will be available to us <br/><br/>if (null != viewState) <br/>{ <br/>currentDate = DateTime.Parse((String) viewState); <br/>} <br/>} <br/><br/>public void RaisePostBackEvent(String eventArgument) <br/>{ <br/>//Page.Response.Write("RaisePostBackEvent Called!!!"); <br/><br/>if (eventArgument == null) <br/>{ <br/>return; <br/>} <br/><br/>// Keep track of old date (for event firing purposes) <br/><br/>DateTime oldDate = currentDate; <br/><br/>// Todo: We should have post-back take two arguments: eventname and eventarguments <br/><br/>if (String.Compare("NavNextMonth", eventArgument, true) == 0) <br/>{ <br/>currentDate = currentDate.AddMonths(1); <br/>} <br/>else if (String.Compare("NavPrevMonth", eventArgument, true) == 0) <br/>{ <br/>currentDate = currentDate.AddMonths(-1); <br/>} <br/>else <br/>{ <br/>int daySelected = Int32.Parse(eventArgument); <br/>currentDate = new DateTime(currentDate.Year, currentDate.Month, daySelected); <br/>} <br/>} <br/><br/>protected override Object SaveState() <br/>{ <br/>// Save CurrentDate out as view state for postback scenarios <br/><br/>return currentDate.ToString(); <br/>} <br/><br/>protected override void Render(HtmlTextWriter output) <br/>{ <br/>//Response.Write(Page.Request.UserAgent); <br/><br/>if (Page.Request.UserAgent.IndexOf("MSIE 5.5") != -1) <br/>RenderUpLevel(output); <br/>else <br/>RenderDownLevel(output); <br/>} <br/><br/>protected void RenderUpLevel(HtmlTextWriter output) <br/>{ <br/>output.WriteLine("&lt;input name='" + UniqueID + "_CurrentDate' id='" + UniqueID + "_CurrentDate' type=hidden&gt;"); <br/>output.WriteLine("&lt;span id='" + UniqueID + "'&gt;&lt;/span&gt;"); <br/>output.WriteLine("&lt;&#115cript language=j&#115cript&gt;drawcalendar('" + UniqueID + "', '" + Int32.Format(currentDate.Year, null) + "/" + Int32.Format(currentDate.Month, null) + "/" + Int32.Format(currentDate.Day, null) + "');&lt;/&#115cript&gt;"); <br/>} <br/><br/>protected override void PreRender() <br/>{ <br/>String DHTMLFunction = ""; <br/><br/>DHTMLFunction += "&lt;&#115cript language='Java&#115cript'&gt; \n"; <br/>DHTMLFunction += " function drawcalendar(calname, newDate) \n"; <br/>DHTMLFunction += " { \n"; <br/>DHTMLFunction += " var CurrentDate = new Date(newDate);\n"; <br/>DHTMLFunction += " var MonthArray = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');\n"; <br/>DHTMLFunction += " var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);\n"; <br/>DHTMLFunction += " var calText;\n"; <br/>DHTMLFunction += " calText = '&lt;table bgcolor=#dcdcdc border=0 height=190 valign=top&gt;';\n"; <br/>DHTMLFunction += " calText = calText + '&lt;tr&gt;&lt;td&gt;';\n"; <br/>DHTMLFunction += " calText = calText + '&lt;center&gt;';\n"; <br/>DHTMLFunction += " calText = calText + \"&lt;a href='java&#115cript:drawcalendar(\\\"\" + calname + \"\\\", \\\"\" + CurrentDate.getFullYear() + \"/\" + CurrentDate.getMonth() + \"/\" + CurrentDate.getDate() + \"\\\")'&gt;\";\n"; <br/>DHTMLFunction += " calText = calText + '&lt;img src=http://www.newasp.net/quickstart/aspplus/images/left4.gif width=11 height=11 border=0&gt;&lt;/a&gt;';\n"; <br/>DHTMLFunction += " calText = cal</p></div><p align="center"><b><font color="red">[1]</font>&nbsp;<a href="200507204916_2.html">[2]</a>&nbsp;<a href="200507204916_3.html">[3]</a>&nbsp;&nbsp;<a href="200507204916_2.html">下一页</a></b></p> <p><a href="javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)" style="text-decoration:none;color:#155da5;display:block;background:url('http://shuqian.qq.com/img/add.gif') no-repeat 0px 0px;height:23px;width:300px;padding:2px 2px 0px 20px;font-size:14px;">将本文收藏到QQ书签与更多好友分享</a></p> <p><script language=javascript src=/wz/sg.JS></script></p> </div> </div> <div class="user_nav"><p>上一篇:<a href="/Article/10/131/2005/200507204915.html">asp+语法教程(六)数据库篇</a></p> <p>下一篇:<a href="/Article/10/131/2005/200507204917.html">asp+语法教程(四)asp+的服务器端编程进介</a> &nbsp;&nbsp;<script language=javascript src=/wz/wangzhai/wangzhai.js></script></p> </div> <div class="list_menu_b"> <div class="left"> <h2>热门阅读 &raquo;</h2> <ul> <li class="showlist1">· <a href="/Article/10/138/2005/200507256912.html" title="注册码大全二">注册码大全二</a></li> <li class="showlist2">· <a href="/Article/10/138/2005/200507256914.html" title="注册码大全四">注册码大全四</a></li> <li class="showlist1">· <a href="/Article/10/138/2005/200507256911.html" title="注册码大全一">注册码大全一</a></li> <li class="showlist2">· <a href="/Article/196/197/2005/2005081911736.html" title="要10G免费网络硬盘的请进来!">要10G免费网络硬盘的请进来!...</a></li> <li class="showlist1">· <a href="/Article/206/2007/2007030319347.html" title="通过google 赶快来赚美金">通过google 赶快来赚美金</a></li> <li class="showlist2">· <a href="/Article/10/138/2005/200507256919.html" title="注册码大全十">注册码大全十</a></li> <li class="showlist1">· <a href="/Article/10/138/2005/200507256913.html" title="注册码大全三">注册码大全三</a></li> <li class="showlist2">· <a href="/Article/14/253/2005/2005092114218.html" title="头像-qq头像(qq新头像)4">头像-qq头像(qq新头像)4</a></li> <li class="showlist1">· <a href="/Article/13/150/2006/2006022316028.html" title="让你轻松架设FTP服务器1">让你轻松架设FTP服务器1</a></li> <li class="showlist2">· <a href="/Article/14/244/2005/2005092014121.html" title="梦幻背景图片7">梦幻背景图片7</a></li> </ul> </div> <div class="right"> <h2>相关阅读 &raquo;</h2> <ul> <li class="showlist11">· <a href="/Article/11/143/2006/2006012615726.html" target="_blank" title="树木枝叶的四种Photoshop抠图方法1">树木枝叶的四种Photosho...</a></li> <li class="showlist12">· <a href="/Article/190/192/2006/2006071716873.html" target="_blank" title="伦敦MeCompany网站设计师谈网页布局艺术2">伦敦MeCompany网站设计师...</a></li> <li class="showlist11">· <a href="/Article/10/133/2005/200507309467.html" target="_blank" title="拼音码表的生成">拼音码表的生成</a></li> <li class="showlist12">· <a href="/Article/10/137/2005/200507181959.html" target="_blank" title="SQL Server数据库安全规划全攻略(2)">SQL Server数据库安全规...</a></li> <li class="showlist11">· <a href="/Article/10/133/2005/200507309260.html" target="_blank" title="在PHP3中实现SESSION的功能(一)">在PHP3中实现SESSION的功...</a></li> <li class="showlist12">· <a href="/Article/10/133/2007/2007073021102.html" target="_blank" title="动态网页技术PHP通过参数来生成MYSQL语句类">动态网页技术PHP通过参数...</a></li> <li class="showlist11">· <a href="/Article/10/133/2005/200507216893.html" target="_blank" title="在 WIN 平台上让你的 Apache 2.0.45 支持 PHP">在 WIN 平台上让你的 Ap...</a></li> <li class="showlist12">· <a href="/Article/11/140/2005/200507181851.html" target="_blank" title="Flash5 有声音的三眼狼(二)">Flash5 有声音的三眼狼(...</a></li> <li class="showlist11">· <a href="/Article/10/131/2005/200507205192.html" target="_blank" title="ado.net数据操作全接触四(表关联,DataAdapter)">ado.net数据操作全接触四...</a></li> <li class="showlist12">· <a href="/Article/11/140/2005/200507181879.html" target="_blank" title="Flash转像素图为矢量图(二)">Flash转像素图为矢量图(...</a></li> </ul> </div> </div> </div> <div id="footer"> <div class="link"> <A href="http://www.jz123.cn/support/about.asp" target="_blank">关于我们</A> | <A href="http://www.jz123.cn/support/help.asp" target="_blank">网站帮助</A> | <A href="http://www.jz123.cn/support/advertise.asp" target="_blank">广告合作</A> | <A href="http://www.jz123.cn/" target="_blank">源码下载</A> | <A href="http://www.jz123.cn/support/sitemap.asp" target="_blank">网站地图</A> | <A href="http://www.jz123.cn/support/declare.asp" target="_blank">下载声明</A> | <font color=red>文章源码投搞:</font>jz123cn@126.com</A> </div> <div class="copyright"> Copyright &copy; 2002-2005 <b>jz123<font color=#ffffff>.cn</font></b></font></a>. All Rights Reserved . <center><script src='http://s6.cnzz.com/stat.php?id=44148&web_id=44148&show=pic' language='JavaScript' charset='gb2312'></script> </div> </div> </div> </body> </html> <span id="naruco_ad_body" style="display:none;"> <script language=javascript src=/adfile/top1.js></script> </span> <script type="text/javascript"> var naruco_ad = document.getElementById('naruco_ad'); if (naruco_ad != null) { naruco_ad.innerHTML=naruco_ad_body.innerHTML; naruco_ad_body.innerHTML=""; } </script>