访问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. Delphi 文件类型

    该内容整理自以下链接 http://www.cnblogs.com/chenyunpeng/archive/2012/08/02/2620513.html 1.DPR: Delphi Project文 ...

  2. Load Runner11录制脚本出现乱码的解决方法

    方法一: 1.录制的脚本出现乱码 Go to Vugen -> Tools -> Recording Options -> Advancedb)   Check the option ...

  3. 检测当前运行环境——移动端与PC端。

    方法1: $(function checkBrowser(){ var browser={ versions:function(){ var u = navigator.userAgent, app ...

  4. JSP动作标签

    动作标签是一种特殊的标签,它影响JSP运行时的功能. (1)include动作标签 <jsp:include page=”文件的名字” />           <%--   不可有 ...

  5. 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口

    解决 把Microsoft.Office.Interop.Excel.DLL的嵌入互操作类型改为ture就可以了

  6. Android doc打开太慢

    C:\Windows\System32\drivers\etc\HOSTS 127.0.0.1 fonts.googleapis.com 127.0.0.1 www.google.com 127.0. ...

  7. mysql 处理查询请求过程

    需要搞清楚查询为什么会慢,就要搞清楚mysql处理查询请求的过程: 1.客户端发送SQL请求给服务器 2.服务器检查是否可以在查询缓存中命中该SQL   查询缓存对SQL性能的影响. 1.需要对缓存加 ...

  8. javascript画直线和画圆的方法(非HTML5的方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 14072202(带IK的Recoil)

    [目标] 带IK的Recoil [思路] 1 继承于USkelControlLimb和UGameSkelCtrl_Recoil 2 效果对比  以这个骨骼为例 Recoil Limb 可见,Recoi ...

  10. 在服务器端将XML转换成HTML

    以下是在服务器上转换XML文件所需要的简单源代码: <% 'Load the XML set xml = Server.CreateObject("Microsoft.XMLDOM&q ...