关于.net的自定义控件(请各位大虾指正)

控件

关于.net的自定义控件
一:谈到自定义控件,就不得不说@Register(这玩艺具体怎么翻译我也不知道,只好用E文,嘿嘿)。
1.@Register语法格式:
<%@Register tagprefix="tagprefix" Namespace="namespace" Assembly="assembly"%> or
<%@Register tagprefix="tagprefix" TagName="tagname" src="pathname"%>
2.属性:
  tagprefix:把别名和名称空间连接在一起
  tagname:把类名和名称空间连接在一起
  Namespace:哈哈,顾名思义把名称空间和tagprefix连接在一起
  src:用户自定义控件的路径
  Assembly:我们与tagprefix联系的名称
  注:Assembly的名称不能包含已存在的文件名
  (E文好的可看随机文档)
二:自定义控件的建立(.ascx)
   .net的自定义控件和asp里的.inc文件非常相似(包括功能也是),我们可以在里面使用html,更可以
连接数据库等等,哎我怎么越说越不明白,还是看看例子吧:
    第一个例子只是html文件:
Header.ascx
<table cellspacing="0" cellpadding="0" width="100%" border="0">
    <tr>
        <td colspan="2" background="images/grid_background.gif" nowrap>
            <table cellspacing="0" cellpadding="0" width="100%" border="0">
                <tr>
                    <td colspan="2">
                        <img src=http://www.163design.net/n/a/"images/most_secretive_place.gif">
                    </td>
                    <td align="right" nowrap>
                        <table cellpadding="0" cellspacing="0" border="0">
                            <tr valign="top">
                                <td align="center" width="65">
                                    <a href="Login.aspx" class="SiteLinkBold"><img src=http://www.163design.net/n/a/"images/sign_in.gif" border="0">
                                        Sign In</a>
                                </td>
                                <td align="center" width="75">
                                    <a href="OrderList.aspx" class="SiteLinkBold"><img src=http://www.163design.net/n/a/"images/account.gif" border="0">
                                        Account</a>
                                </td>
                                <td align="center" width="55">
                                    <a href="ShoppingCart.aspx" class="SiteLinkBold"><img src=http://www.163design.net/n/a/"images/cart.gif" border="0">
                                        Cart</a>
                                </td>
                                <td align="center" width="65">
                                    <a href="InstantOrder.asmx" class="SiteLinkBold"><img src=http://www.163design.net/n/a/"images/services.gif" border="0">
                                        Services</a>
                                </td>
                            <tr>
                        </table>
                    </td>
                    <td width="10">
                         
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="2" nowrap>
            <form method="post" action="SearchResults.aspx" id="frmSearch" name="frmSearch">
                <table cellspacing="0" cellpadding="0" width="100%" border="0">
                    <tr bgcolor="#9D0000">
                        <td background="images/modernliving_bkgrd.gif">
                            <img align="left" src=http://www.163design.net/n/a/"images/modernliving.gif">
                        </td>
                        <td width="94" align="right" bgcolor="#9D0000">
                            <img src=http://www.163design.net/n/a/"images/search.gif">
                        </td>
                        <td width="120" align="right" bgcolor="#9D0000">
                            <input type="text" name="txtSearch" ID="txtSearch" SIZE="20">
                        </td>
                        <td align="left" bgcolor="#9D0000">
                             <input type="image" src=http://www.163design.net/n/a/"images/arrowbutton.gif" border="0" id="image1" name="image1">
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
</table>
这里没什么可说的,大家都非常熟.
    第二个例子(和上一个当然不一样了!):
Menu.ascx
<%@ Control Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

    //*******************************************************
    //
    // The Page_Load event on this page is used to obtain
    // from a database a list of all product categories
    // and databind it to an asp:datalist control.
    //
    // To optimize performance, this user control is output
    // cached (varying based on the categoryId and selection
    // passed through the querystring.    
    //
    //*******************************************************

    void Page_Load(Object sender, EventArgs e) {
       
        // Set the curent selection of list
        String selectionId = Request.Params["selection"];

        if (selectionId != null) {
            MyList.SelectedIndex = Int32.Parse(selectionId);
        }

        // Obtain list of menu categories and databind to list control
        IBuySpy.ProductsDB products = new IBuySpy.ProductsDB();
        
        MyList.DataSource = products.GetProductCategories();
        MyList.DataBind();      
    }

</script>

<table cellspacing="0" cellpadding="0" width="145" border="0">
    <tr valign="top">
        <td colspan="2">
            <a href="default.aspx"><img src=http://www.163design.net/n/a/"images/logo.gif" border="0"></a>
        </td>
    </tr>
    <tr valign="top">
        <td colspan="2">
            <asp:DataList id="MyList" runat="server" cellpadding="3" cellspacing="0" width="145" SelectedItemStyle-BackColor="dimgray" EnableViewState="false">
                <ItemTemplate>
                    <asp:HyperLink class="MenuUnselected" id="HyperLink1" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>' NavigateUrl='<%# "productslist.aspx?CategoryID=" + DataBinder.Eval(Container.DataItem, "CategoryID") + "&selection=" + Container.ItemIndex %>' runat="server" />
                </ItemTemplate>
                <SelectedItemTemplate>
                    <asp:HyperLink class="MenuSelected" id="HyperLink2" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>' NavigateUrl='<%# "productslist.aspx?CategoryID=" + DataBinder.Eval(Container.DataItem, "CategoryID") + "&selection=" + Container.ItemIndex %>' runat="server" />
                </SelectedItemTemplate>
            </asp:DataList>
        </td>
    </tr>
    <tr>
        <td width="10">
             
        </td>
        <td>
            <br><br><br><br><br><br>
            <a href="docs/docs.htm" target="_blank" class="SiteLink">IBuySpy Store<br>Documentation</a>
        </td>
    </tr>
</table>
三:我们建好了两个.ascx文件,也就我们自己的控件,那怎么用呢?
   看下面:
default.aspx
<%@ Page Language="C#" %>
<%@ Register TagPrefix="IBuySpy" TagName="Menu" Src="_Menu.ascx" %>
<%@ Register TagPrefix="IBuySpy" TagName="Header" Src="_Header.ascx" %>
<script runat="server">

    //*******************************************************
    //
    // The Page_Load event on this page is used to personalize
    // the welcome message seen by returning IBuySpy users.
    // It does this by retrieving a client-side cookie
    // (persisted on the client in the Login.aspx and
    // register.aspx pages) and updating a label control.
    //
    //*******************************************************

    void Page_Load(Object sender, EventArgs e) {

        // Customize welcome message if personalization cookie is present
        if (Request.Cookies["IBuySpy_FullName"] != null) {
            WelcomeMsg.Text = "Welcome " + Request.Cookies["IBuySpy_FullName"].Value;
        }
    }

</script>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="IBuySpy.css">
    </head>
    <body background="images/sitebkgrdnogray.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">
        <table cellspacing="0" cellpadding="0" width="100%" border="0">
            <tr>
                <td colspan="2">
                    <IBuySpy:Header ID="Header1" runat="server" />
                </td>
            </tr>
            <tr>
                <td valign="top" width=145>
                    <IBuySpy:Menu id="Menu1" runat="server" />
                    <img height="1" src=http://www.163design.net/n/a/"images/1x1.gif" width="145">
                </td>
                <td align="left" valign="top" width="*" nowrap>
                    <table height="100%" align="left" cellspacing="0" cellpadding="0" width="100%" border="0">
                        <tr valign="top">
                            <td nowrap>
                                <br>
                                <img align="left" width="24" SRC=http://www.163design.net/n/a/"images/1x1.gif">
                                <table cellspacing="0" cellpadding="0" width="100%">
                                    <tr>
                                        <td>
                                            <table cellspacing="0" cellpadding="0" width="100%">
                                                <tr>
                                                    <td class="HomeHead">
                                                        <asp:Label id="WelcomeMsg" runat="server">Welcome to IBuySpy.com</asp:Label>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>

本鸟是边学边看,不足之处请大虾指点(jiabaoxu@china.com),谢谢!!
                                                                                                      CNet
                                                                                                      2000.7.30

时间: 2024-10-03 01:25:21

关于.net的自定义控件(请各位大虾指正)的相关文章

sap修改采购订单的项目文本,怎么读取修改之前的文本内容,请各位大虾帮忙。

问题描述 sap修改采购订单的项目文本,怎么读取修改之前的文本内容,请各位大虾帮忙. 修改了一个采购订单的项目文本,用函数read_text 只能读取修改之后的文本内容,请问怎么读取修该之前的文本内容,紧急阿.我试过free memory .但是会影响其它文本.请大家忙帮. 解决方案 SAP的字段和对象的修改都会保存旧值,数据保存在CDHDR和CDPOS表中,对于提取旧值你可以采用两种方法 1)使用sap的标准函数CHANGEDOCUMENT_READ_HEADERS 和CHANGEDOCUME

请各位大虾推荐flash入门书籍

问题描述 本人因公司开发网站需要用到flash,类似 QQ秀那种效果, 要求是as3什么的, 从来没弄过flash这块 不知从哪里入手, 请各位大虾推荐基本比较好的入门书籍, PS: 需要和java做数据交换的, 不过flash是单独的语言,应该没啥局限性, 主要是入门 入门, 让我知道flash是个啥东西,怎么开始编码 3Q3Q3Q3X 解决方案 如果单单是as3的话,推荐这本http://ishare.iask.sina.com.cn/f/18195263.html你说的数据交互应该是指的a

把一个网站的网址嵌入到我的网页框架中去后,点击菜单失去链接效果,不知何解,请各位大虾帮帮忙吧

问题描述 我也遇到同样一个问题,把一个网站的网址嵌入到我的网页框架中去后,点击菜单失去链接效果,不知何解,请各位大虾帮帮忙吧鼠标点击网页任务栏中出现:javascript:__doPostBack('LeftMenu1','Traffic')的快捷方式类似http://www.elong.com的网站也是就出现了一样的结果我要想用谁能给我想想法子 解决方案 解决方案二:问题已解决是因为隐藏框架导致的!解决方案三:该回复于2009-09-28 09:39:42被版主删除

请各位大虾帮忙。拓扑排序求关键路径。用c++编写。跪求。

问题描述 请各位大虾帮忙.拓扑排序求关键路径.用c++编写.跪求. 解决方案 解决方案二:c++你跑这里

请各位大虾帮帮忙

问题描述 小弟在一个培训基地在培训java,还有一个多月就结束了,听同学说,现在groovy和Grails框架非常流行,趁着还没毕业这段时间,想看groovy和Grails方面的书籍,学习一下,请各位大虾们帮忙推荐几本比较流行的.比较好的这方面的书籍,最好是电子版的,还有下载网址.毕竟现在我还没参加工作,兜里的money还是吝啬了点.谢谢各位大侠了~~

百万商业圈小型企业云ERP总体开发规划,请大家批评指正

问题描述 百万商业圈小型企业云ERP总体开发规划请大家批评指正一.开发目标1.实现能让众多企业在线使用2.实现一个企业多个帐套.多个用户在线使用3.实现企业按需以服务方式进行购买二.开发环境.开发工具.数据库.编译环境.部署环境1.开发环境:windowsxp.windows2000或windows20032.开发工具:MicrosoftVisualStudio20083.数据库:Oracle或SqlServer2005.SqlServer2008或MySql4.编译环境:Ubuntu.Cent

DrawString 异常。实在找不 到 问题,烦请各位大虾指点。

问题描述 问题:m_bufGrap.Graphics.DrawString(str2,drawFont,drawBrush,20.0F,30.0F)未处理的"System.ArgumentException"类型的异常出现在System.Drawing.dll中.其他信息:参数无效.代码:DimdrawFontAsNewFont("Arial",16)DimdrawBrushAsNewSolidBrush(Color.Blue)Dimstr2AsString=&qu

新手,请众大虾举荐适合的示例以供学习asp.net

问题描述 petshop好像有点复杂...我想跟着示例做,请推荐.. 解决方案 解决方案二:thebeerhouse解决方案三:anymore?解决方案四:想学习的话,推荐<天轰穿>的视频教程,一共有81章,FLASH格式,文件大概一共有300M左右,并不大,课程由浅入深,很容易看懂.不过上面没有数据层的操作,都是页面和逻辑层的,能把这些学会,NET就懂得一半了地址:http://thcjp.cnblogs.com/解决方案五:petshop好像有点复杂...=================

抄来之作!饼图和直方图的制作,稍后会提供我的作品,请各位大侠指正!

饼图 在 Web 页面中使用图表(chart)表现数据       在 Web 编程中经常需要做的一件事情就是把从数据库中查出的数据(数字)使用图表(chart)的形式在页面中表现出来.下面我们简单总结几种常见的做法. 1. 如果图表的样式只需要柱形图(bar)就可以的话,有一种非常简单,偷懒的方法,即使用某些 tag 的 width 属性来表现就可以.举例如下: <IMG HEIGHT=5 WIDTH=<%= 数值 %> SRC=http://www.163design.net/a/q