网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

.net的自定义控件

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 14/02/25

鉴于大家对ASP.net十分关注,我们编辑小组在此为大家搜集整理了“.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="/uploadfile/201402/25/1D141550407.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="/uploadfile/201402/25/25141551794.gif" border="0">
                                        Sign In</a>
                                </td>
                                <td align="center" width="75">
                                    <a href="OrderList.aspx" class="SiteLinkBold"><img src="/uploadfile/201402/25/FD141553555.gif" border="0">
                                        Account</a>
                                </td>
                                <td align="center" width="55">
                                    <a href="ShoppingCart.aspx" class="SiteLinkBold"><img src="/uploadfile/201402/25/5B141554192.gif" border="0">
                                        Cart</a>
                                </td>
                                <td align="center" width="65">
                                    <a href="InstantOrder.asmx" class="SiteLinkBold"><img src="/uploadfile/201402/25/21141555765.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="/uploadfile/201402/25/DA141556509.gif">
                        </td>
                        <td width="94" align="right" bgcolor="#9D0000">
                            <img src="/uploadfile/201402/25/F0141556357.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="/uploadfile/201402/25/04141556640.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="/uploadfile/201402/25/78141556398.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="/uploadfile/201402/25/D1141557464.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="/uploadfile/201402/25/61141557865.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






  • 下一篇资讯: DataList控件也玩分页
  • 网学推荐

    免费论文

    原创论文

    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号