033. asp.netWeb用户控件之二将页面转换成web控件和使用Web控件显示热点新闻
访问Web用户控件的属性
ASP.NET提供的各种服务器控件都有其自身的属性和方法,程序开发人员可以灵活地使用服务器控件中的属性和方法开发程序。在用户控件中,程序开发人员也可以自行定义各种属性和方法,从而灵活地应用用户控件。
this.Label1.Text = this.WebUserControl1.str_userName.ToString();
访问Web用户控件中的服务器控件
程序开发人员可以在用户控件中添加各种控件,如Label控件、TextBox控件等,但当用户控件创建完成后,将其添加到网页时,在网页的后台代码中不能直接访问用户控件中的服务控件的属性。为了实现对用户控件中的服务器控件的访问,必须在用户控件中定义公有属性,并且利用get访问器与set访问器来读取、设置控件的属性。
将Web网页转换为用户控件:
用户控件与Web网页的设计几乎完全相同,因此,如果某个Web网页完成的功能可以在其他Web页中重复使用,可以直接将Web网页转化成用户控件,而无须再重新设计。
将Web网页转化成用户控件,需要进行以下操作:
(1) 在.aspx(Web网页的扩展名)文件的HTML视图中,删除<html>、<head>、<body>以及<form>等标记。
(2)将@Page指令修改为@Control,并将Codebehind属性修改成以.ascx.cs为扩展名的文件。例如,原Web网页中的代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
需要修改为:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Default.ascx.cs" Inherits=" WebUserControl " %>
(3)在后台代码中,将public class声明的页类删除,改为用户控件的名称,并且将System.Web. UI.Page改为System.Web.UI.UserControl。例如:
public partial class _Default : System.Web.UI.Page
需要修改为:
public partial class WebUserControl : System.Web.UI.UserControl
(4)最后,在解决方案资源管理器中,将文件的扩展名从.aspx修改为.ascx,其代码后置文件会随之改变,从.aspx.cs改变为.ascx.cs。
使用Web控件显示热点新闻:
web控件代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ShowHotspotNews.ascx.cs" Inherits="ShowHotspotNews" %>
<style type="text/css">
.style1
{
width: 22%;
height: 115px;
}
.style2
{
height: 38px;
}
.style3
{ width: 21px;
}
.style4
{
height: 2px;
}
</style>
<script type="text/javascript">
function show(o) {
window.open("ShowInfo.aspx?id="+o);
}
</script>
<table cellpadding="0" cellspacing="0" class="style1"
style="border: 1px solid #999999">
<tr>
<td class="style2" colspan="2"
style="border-bottom: 1px solid #999999; text-align: center; border-left-color: #999999; border-left-width: 1px; border-right-color: #999999; border-right-width: 1px; border-top-color: #999999; border-top-width: 1px;">
畅销产品</td>
</tr>
<tr>
<td align="left" class="style3" rowspan="2" style="text-align: left"
valign="top">
</td>
<td align="left" class="style4" style="text-align: left" valign="top">
</td>
</tr>
<tr>
<td align="left" style="text-align: left" valign="top">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</td>
</tr>
</table>
Default.aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register src="ShowHotspotNews.ascx" tagname="ShowHotspotNews" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>默认首页</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<uc1:ShowHotspotNews ID="ShowHotspotNews1" runat="server" />
</form>
</body>
</html>
ShowInfo.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowInfo.aspx.cs" Inherits="ShowInfo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>详细信息</title>
<style type="text/css">
.style1
{
width: 32%;
height: 154px;
}
.style2
{
width: 90px;
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" cellpadding="0" cellspacing="0" class="style1">
<tr>
<td class="style2">
编号:</td>
<td>
<asp:Label ID="lblid" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
标题:</td>
<td>
<asp:Label ID="lbltitle" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
内容:</td>
<td>
<asp:Label ID="lblcontent" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
日期:</td>
<td>
<asp:Label ID="lbldate" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
ShowInfo.apsx.cs代码:
public partial class ShowInfo : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123.456;database=TYW;");
protected void Page_Load(object sender, EventArgs e)
{
int id = int.Parse(Request.QueryString["id"].ToString());
SqlDataAdapter sda = new SqlDataAdapter("select * from category where cateID=" + id + "", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
lblid.Text = ds.Tables[].Rows[]["cateID"].ToString();
lbltitle.Text = ds.Tables[].Rows[]["cateName"].ToString();
lblcontent.Text = ds.Tables[].Rows[]["orderBy"].ToString();
lbldate.Text = ds.Tables[].Rows[]["flag"].ToString();
}
}
StyleSheet.css代码:
body {
}
a{
color:#0072A8;
font-family:新宋体;
font-size:12px;
text-decoration:none;
}
a:visited
{
color:Gray;
font-family:新宋体;
font-size:12px;
text-decoration:none;
}
a:link
{
color:#0072A8;
font-family:新宋体;
font-size:12px;
text-decoration:none;
}
a:active
{
color:#0072A8;
font-family:新宋体;
font-size:12px;
text-decoration:none;
}
a:hover
{
color:Red;
font-family:新宋体;
font-size:12px;
text-decoration:underline;
}
最终效果:

033. asp.netWeb用户控件之二将页面转换成web控件和使用Web控件显示热点新闻的更多相关文章
- 039. asp.netWeb用户控件之七实现具有虚拟键盘的功能的用户控件
用户控件ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="K ...
- 038. asp.netWeb用户控件之六实现日期选择的用户控件
web用户控件的ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...
- 037. asp.netWeb用户控件之五使用用户控件实现文件上传功能
fileUpload.ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile= ...
- 036. asp.netWeb用户控件之五使用用户控件实现分页数据导航
UserDataPager.ascx用户控件代码: <%@ Control Language="C#" AutoEventWireup="true" Co ...
- 035. asp.netWeb用户控件之四通过用户控件实现投票和结果分析
用户控件Vote.ascx代码 <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...
- 034. asp.netWeb用户控件之三通过用户控件实现用户注册和登录
用户控件login.ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=& ...
- 032. asp.netWeb用户控件之一初识用户控件并为其自定义属性
Web用户控件的优点: 可以将常用的内容或者控件以及控件的运行程序逻辑, 设计为用户控件, 以后便可以在多个页面中重复使用该用户控件, 从而省去许多重复性的工作. 如网页上的导航栏, 几乎每个页面都需 ...
- ATL控件签名之后页面还提示“在此页面上的Activex控件和本页上的其他部分及交互可能不安全”
ATL控件正常签名打包,然后安装之后还是会提示: 没理由啊,签名是花钱搞得正当的签名.后来查了资料才知道这还不够,需要在创建ATL控件的时候继承一个IObjectSafetyImpl 类 知道了原因, ...
- Winform控件输入的字母转换成大写
private void textBoxHbh_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 'a' & ...
随机推荐
- SpringMVC -- 梗概--贰
1.为什么要配置: mvc:annotation-driven 1>在springMVC的处理流程中,有两个重要组件:HandlerMapping和HandlerAdapter 分别负责解析Ha ...
- js javascript 模拟点击 超级链接点击 转
转自:http://mo2g.com/view/42/ 我尝试过多次用jQuery模拟用户点击a标签的功能,但都没有成功,并且困扰了很久.前段时间的一次发呆,冒出了新的想法,于是就动手进行了测试. 先 ...
- windows 下配置 Nginx 常见问题(转)
windows 下配置 Nginx 常见问题 因为最近的项目需要用到负载均衡,不用考虑,当然用大名鼎鼎的Nginx啦.至于Nginx的介绍,这里就不多说了,直接进入主题如何在Windows下配置. 我 ...
- app——分享wap站,数据处理页面展示
无意中接到了一个小的工作任务:配合手机app端的分享功能做一个wap站,简言之:将手机app端分享的文章id传过来,利用此id再进行一系列的操作,由于文章分为纯文本,图文以及图集的三种类型的文章,因此 ...
- android代码嵌入html代码
有时候需要在一个TextView控件中设置两种不同颜色的字体,这时候可以使用Html.fromHtml方法实现 例如: tvTaskDesc.setText(Html.fromHtml("当 ...
- WCF服务开发与调用的完整示例
WCF服务开发与调用的完整示例 开发工具:VS2008 开发语言:C# 开发内容:简单的权限管理系统 第一步.建立WCF服务库 点击确定,将建立一个WCF 服务库示例程序,自动生成一个包括IServi ...
- php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
- Libgdx 开发指南(1.1) 应用框架——生命周期
生命周期 Libgdx应用有一个定义好的生命周期,控制着整个应用的状态,例如creation, pausing, resuming, disposing ApplicationListener 开发者 ...
- “cv”: 具有该名称的命名空间不存在
记得添加#include<highgui.h> 无法解析的外部符号 遇到这种问题一般都是由于缺少相应的库文件 右击项目,选择“属性”--“链接器”--“输入”--“附加依赖项” 根据错误中 ...
- 在线工具、setHtmlRem、px2rem
http://tool.lu/c/developer 开发类在线工具 https://github.com/leon776/setHtmlRem setHtmlRem https://githu ...