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' & ...
随机推荐
- hdu 5358 First One
题目链接:hdu 5358 思路不难理解,就是个尺取法而已,floor(log2X) + 1 就是求 X 的二进制表示的位数,对于题目来说这个值最多只是 30+,从这里入手开始枚举,运用尺取法可以达到 ...
- Spring事务管理只对出现运行期异常进行回滚
原文:http://blog.csdn.net/abc19900828/article/details/39497631 使用spring难免要用到spring的事务管理,要用事务管理又会很自然的选择 ...
- 转!!URL 传+号到后台变空格问题解决方案
网上很多解决方法,但是前提是get请求(或者是post请求后面追加的参数),让我试了很久(我是post),没成功!引以为戒!! 今天在调试客户端向服务器传递参数时,参数中的“+”全部变成了空格,原因是 ...
- js 过滤script
function stripscript(s) { return s.replace(/<script.*?>.*?<\/script>/ig, ''); } 稍微 ...
- 003_kafka_主要配置
1.broker配置 2.Producer主要配置 3.Consumer主要配置 出错提示信息: replication factor: 1 larger than available brokers ...
- C++学习之:括号匹配与栈的使用
#include <stack> using std::stack ; 变量定义: stack<T> stackName ; 成员函数: 成员函数 功能 bool empt ...
- vs2012中VC连接mysql
在MySQL的安装目录下你会找到include和lib文件夹(完全安装模式下),里面分别是C接口的头文件和库文件,库文件只用libmysql.lib就好了,即把libmysql.lib拷贝到了VC++ ...
- MVC——入门+简单的小实例
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controler)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据显示分离的方法. ...
- Reflector反编译WinForm程序重建项目资源和本地资源
工具:vs2012..NET Reflector8.1.0.35 要解决的问题: 通过Reflector反编译生成的代码可以编译通过并显示窗体的本地资源和项目资源图片 一.测试项目 两个图片分别放在项 ...
- iOS开发UI篇—控制器的View的创建
iOS开发UI篇—控制器的View的创建 一.6种创建控制器View的方式 #import "NJAppDelegate.h" #import "NJViewContro ...