访问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">
&nbsp;&nbsp;&nbsp;&nbsp;
</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控件显示热点新闻的更多相关文章

  1. 039. asp.netWeb用户控件之七实现具有虚拟键盘的功能的用户控件

    用户控件ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="K ...

  2. 038. asp.netWeb用户控件之六实现日期选择的用户控件

    web用户控件的ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  3. 037. asp.netWeb用户控件之五使用用户控件实现文件上传功能

    fileUpload.ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile= ...

  4. 036. asp.netWeb用户控件之五使用用户控件实现分页数据导航

    UserDataPager.ascx用户控件代码: <%@ Control Language="C#" AutoEventWireup="true" Co ...

  5. 035. asp.netWeb用户控件之四通过用户控件实现投票和结果分析

    用户控件Vote.ascx代码 <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  6. 034. asp.netWeb用户控件之三通过用户控件实现用户注册和登录

    用户控件login.ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=& ...

  7. 032. asp.netWeb用户控件之一初识用户控件并为其自定义属性

    Web用户控件的优点: 可以将常用的内容或者控件以及控件的运行程序逻辑, 设计为用户控件, 以后便可以在多个页面中重复使用该用户控件, 从而省去许多重复性的工作. 如网页上的导航栏, 几乎每个页面都需 ...

  8. ATL控件签名之后页面还提示“在此页面上的Activex控件和本页上的其他部分及交互可能不安全”

    ATL控件正常签名打包,然后安装之后还是会提示: 没理由啊,签名是花钱搞得正当的签名.后来查了资料才知道这还不够,需要在创建ATL控件的时候继承一个IObjectSafetyImpl 类 知道了原因, ...

  9. Winform控件输入的字母转换成大写

    private void textBoxHbh_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 'a' & ...

随机推荐

  1. Knockout学习笔记之二($root,$parent及$data的区别)

    以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...

  2. zabbix3.0.4 部署之三 (LNMP > Mysql 安装)

    MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具. 因此,我们首先要在系统中源码编译安装cmake工具. 接下来的安装过程中会遇到错误,我们 ...

  3. HTTP状态码及其含义

    下表显示了常见的HTTP 1.1状态代码以及它们对应的状态信息和含义.应当谨慎地使用那些只有HTTP 1.1支持的状态代码,因为许多浏览器还只能够支持HTTP 1.0.如果你使用了HTTP 1.1特有 ...

  4. vue.js学习笔记之v-bind,v-on

    v-bind 指令用于响应地更新 HTML 特性 形式如:v-bind:href    缩写为    :href; v-on 指令用于监听DOM事件 形式如:v-on:click  缩写为 @clic ...

  5. free函数

    free函数 原型: void free(void *ptr) 功 能: 释放ptr指向的存储空间.被释放的空间通常被送入可用存储区池,以后可在调用malloc.realloc以及calloc函数来再 ...

  6. 关于如何写UI及屏幕适配的一些技巧

    因为公司开启了一个新的iOS项目, 所以近期比较忙, 没有更新博客,今天打算总结一下关于UI布局及屏幕适配的一些实战技巧,尤其使用纯代码,会对提升效率及代码易于维护等方面有明显帮助,这里提到的没有使用 ...

  7. mysql 配置 utf8 依然乱码

    mysql 乱码问题排除方案: 1.检查数据库及数据表是不是utf8字符集 2.查看一下jdbc.properties配置的数据库url 是否配置了characterEncoding=UTF-8或者在 ...

  8. 读javascript高级程序设计15-Ajax,CORS,JSONP,Img Ping

    平时用惯了jQuery.ajax之类的方法,却时常忽略了它背后的实现,本文是学习了AJAX基础及几种跨域解决方案之后的一些收获. 一.AJAX——XMLHttpRequest 谈起Ajax我们都很熟悉 ...

  9. java_easyui体系之DataGrid(3)[转]

    一:简介 在2的基础上实现增删改.增.改都是通过行编辑来实现的.具体业务逻辑: 1. 增加一条记录的时候: a) 在datagrid表格最前端增加一行(当然也可在指定位置增加.)增加的行的字段必须与要 ...

  10. iOS开发Swift篇—(三)字符串和数据类型

    iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容  let website = "http://www ...