关于.net的自定义控件(请各位大虾指正)
来源:互联网 发布:微信群加好友软件 编辑:程序博客网 时间:2024/04/26 18:31
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
一:谈到自定义控件,就不得不说@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="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="images/sign_in.gif" border="0">
Sign In</a>
</td>
<td align="center" width="75">
<a href="OrderList.aspx" class="SiteLinkBold"><img src="images/account.gif" border="0">
Account</a>
</td>
<td align="center" width="55">
<a href="ShoppingCart.aspx" class="SiteLinkBold"><img src="images/cart.gif" border="0">
Cart</a>
</td>
<td align="center" width="65">
<a href="InstantOrder.asmx" class="SiteLinkBold"><img src="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="images/modernliving.gif">
</td>
<td width="94" align="right" bgcolor="#9D0000">
<img src="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="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="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="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>共2页: 上一页 1 [2] 下一页 <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
关于.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="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="images/sign_in.gif" border="0">
Sign In</a>
</td>
<td align="center" width="75">
<a href="OrderList.aspx" class="SiteLinkBold"><img src="images/account.gif" border="0">
Account</a>
</td>
<td align="center" width="55">
<a href="ShoppingCart.aspx" class="SiteLinkBold"><img src="images/cart.gif" border="0">
Cart</a>
</td>
<td align="center" width="65">
<a href="InstantOrder.asmx" class="SiteLinkBold"><img src="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="images/modernliving.gif">
</td>
<td width="94" align="right" bgcolor="#9D0000">
<img src="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="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="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="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>共2页: 上一页 1 [2] 下一页 <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 关于.net的自定义控件(请各位大虾指正)
- 我对自定义控件和用户控件的理解(不对的地方请大家指正!)
- 请各位大虾们帮帮小弟,谢谢!一个关于产品搜索数据库设计思路的问题
- 请各位大虾多多指教!
- 请各位大虾多多指教
- window xp安装不了的问题,请各位大虾指教
- 如何锻炼分析问题的方法?请各位大虾指教~~
- ASP.NET数据分页(使用viewstate)望大虾指正
- 请各位大虾帮忙!小女子谢过了!:)
- [VB.NET]关于进销存“移动加权平均”的问题,请教各位大虾,
- 20171216区间dp(有错请各位大佬指正)
- 关于SOA的理解,请大家指正
- 急急急,请各位大虾帮帮忙!
- 请各位大虾指教,字符串问题
- 请各位大虾进来解决下~!急
- 有个关于DetailsView的问题请教各位大虾
- [VB.NET]请大虾帮忙!!
- 小弟求助:关于自定义web控件的属性问题。。大虾请进。。
- 用PHP和ACCESS写聊天室(二)
- C#:Web Service异常处理
- 一个简易需要注册的留言版程序
- 保证应用程序只有一个实例在运行
- 一个分页的论坛
- 关于.net的自定义控件(请各位大虾指正)
- 图片存储与浏览一例(Linux+Apache+PHP+MySQL)
- singleton设计模式的C#实现(上)
- 下载文件的点击数回填
- 用C#设计一个命令行方式的文件分割器
- Banner程序
- 用C#编写获取远程IP,MAC的方法
- 全面測試email的有效性
- C#调用控制面板选项