在用户控件中自定义属性

有段时间没写什么博客了,一直忙于工作上的事情,白天上班,晚上上课(充电啊!~~)

想了想,还是学学微软的很多“讲师”,多写些入门级的技术文章吧,简单易懂,对很多朋友有帮助,看网上有关用户控件的文章比较少,就决定写一篇。

首先,在ASP.NET中,用户控件有几个概念必须弄清楚:

1、它的后缀名为.ASCX
2、它本身不能直接执行
3、它可以由用户自定义它的属性和事件

例如,我们先创建一个用户控件文件,名为 TxtData.ascx ,代码如下:

 1在用户控件中自定义属性在用户控件中自定义属性<%在用户控件中自定义属性@ Control Language="C#" AutoEventWireup="true" CodeFile="TxtData.ascx.cs" Inherits="TxtData" %>
 2在用户控件中自定义属性<link href="CSS/CSS.css" rel="stylesheet" type="text/css" />
 3在用户控件中自定义属性在用户控件中自定义属性<style type="text/css">在用户控件中自定义属性
 4在用户控件中自定义属性<!--
 5在用户控件中自定义属性在用户控件中自定义属性.STYLE1 {在用户控件中自定义属性}{
 6在用户控件中自定义属性    font-size: 14px;
 7在用户控件中自定义属性    font-weight: bold;
 8在用户控件中自定义属性}

 9在用户控件中自定义属性-->
10在用户控件中自定义属性
</style>
11在用户控件中自定义属性<table width="100%" border="0" cellpadding="0" cellspacing="0" class="LeftBorder">
12在用户控件中自定义属性  <tr>
13在用户控件中自定义属性    <td height="30" bgcolor="#C4DBEE" >
14在用户控件中自定义属性        <span class="STYLE1">
15在用户控件中自定义属性            <asp:Label ID="ClassName" runat="server" Text="Label"></asp:Label>
16在用户控件中自定义属性        </span>
17在用户控件中自定义属性        </td>
18在用户控件中自定义属性  </tr>
19在用户控件中自定义属性  <tr>
20在用户控件中自定义属性    <td>
21在用户控件中自定义属性<asp:DataList ID="DataList1" runat="server" Font-Size="Small" Width="100%" >
22在用户控件中自定义属性    <ItemTemplate>
23在用户控件中自定义属性        <table width=160 border="0">
24在用户控件中自定义属性              <TR vAlign=top>
25在用户控件中自定义属性                <TD vAlign=top align=middle height=6 rowSpan=2>
26在用户控件中自定义属性                <IMG height=7 src="images/row.gif" width=8 > 
27在用户控件中自定义属性                </TD>
28在用户控件中自定义属性                <TD align=left  height=12>
29在用户控件中自定义属性在用户控件中自定义属性                <%在用户控件中自定义属性# GetTxtLink("TxtDataView.aspx?ID=" + (int)Eval("ID"), Eval("TxtTitle").ToString(), 10)%>
30在用户控件中自定义属性    
31在用户控件中自定义属性                </TD>
32在用户控件中自定义属性           </TR>
33在用户控件中自定义属性
34在用户控件中自定义属性        </table>
35在用户控件中自定义属性    </ItemTemplate>
36在用户控件中自定义属性
37在用户控件中自定义属性</asp:DataList>
38在用户控件中自定义属性    </td>
39在用户控件中自定义属性  </tr>
40在用户控件中自定义属性</table>


上面在用户控件中,包含了个DataList控件,其实也你也完全可以用HTML语言的形式,利用TABL+ TR+TD的表格代码来实现,在其中加入数据绑定,就可以了,并且很轻松地就实现了,我们在这里利用DataList,是为了更好地利用.NET平台中一些控件,来充实程序的可扩展性和易用性。当然,你也可以使用Lable标签控件,使用VS中现成的控件,在实现面向对象编程上,是会带来方便的。

后台代码文件 TxtData.ascx.cs 如下:

 1在用户控件中自定义属性using System;
 2在用户控件中自定义属性using System.Data;
 3在用户控件中自定义属性using System.Configuration;
 4在用户控件中自定义属性using System.Collections;
 5在用户控件中自定义属性using System.Web;
 6在用户控件中自定义属性using System.Web.Security;
 7在用户控件中自定义属性using System.Web.UI;
 8在用户控件中自定义属性using System.Web.UI.WebControls;
 9在用户控件中自定义属性using System.Web.UI.WebControls.WebParts;
10在用户控件中自定义属性using System.Web.UI.HtmlControls;
11在用户控件中自定义属性
12在用户控件中自定义属性public partial class TxtData : System.Web.UI.UserControl
13在用户控件中自定义属性在用户控件中自定义属性在用户控件中自定义属性{
14在用户控件中自定义属性    private String _TxtClass = "";  //定义属性变量
15在用户控件中自定义属性
16在用户控件中自定义属性    public String TxtClass          //定义属性
17在用户控件中自定义属性在用户控件中自定义属性    在用户控件中自定义属性{
18在用户控件中自定义属性
19在用户控件中自定义属性        get                     //get方法
20在用户控件中自定义属性在用户控件中自定义属性        在用户控件中自定义属性{
21在用户控件中自定义属性            return _TxtClass;
22在用户控件中自定义属性        }

23在用户控件中自定义属性        set                     //set方法
24在用户控件中自定义属性在用户控件中自定义属性        在用户控件中自定义属性{
25在用户控件中自定义属性            _TxtClass = value;//取得前台页面中,用户设定的属性值
26在用户控件中自定义属性            LixyClass TxtSql = new LixyClass();
27在用户控件中自定义属性            this.DataList1.DataSource = TxtSql.ExecuteSqlRead("select * FROM TxtData where TxtClass='" + this.GetClassID(_TxtClass) + "' order by id desc");
28在用户控件中自定义属性            this.DataList1.DataBind();//按照用户设定的值,取出数据,并绑定到控件
29在用户控件中自定义属性        }

30在用户控件中自定义属性    }

31在用户控件中自定义属性
32在用户控件中自定义属性    protected void Page_Load(object sender, EventArgs e)
33在用户控件中自定义属性在用户控件中自定义属性    在用户控件中自定义属性{
34在用户控件中自定义属性        if (!IsPostBack)
35在用户控件中自定义属性在用户控件中自定义属性        在用户控件中自定义属性{
36在用户控件中自定义属性            this.ClassName.Text = this.TxtClass;//将控件表头名称赋值,值为用户设定的值
37在用户控件中自定义属性        }

38在用户控件中自定义属性    }

39在用户控件中自定义属性
40在用户控件中自定义属性在用户控件中自定义属性    /**//// <summary>
41在用户控件中自定义属性    /// 获取指定的字符串长度
42在用户控件中自定义属性    /// </summary>
43在用户控件中自定义属性    /// <param name="Url">文字链接</param>
44在用户控件中自定义属性    /// <param name="Title">主题字符串</param>
45在用户控件中自定义属性    /// <param name="Count">长度</param>
46在用户控件中自定义属性    /// <returns></returns>

47在用户控件中自定义属性    protected static string GetTxtLink(string Url, string Title, int Count)
48在用户控件中自定义属性在用户控件中自定义属性    在用户控件中自定义属性{
49在用户控件中自定义属性        return LixyClass.GetTxtLink(Url, Title, Count);
50在用户控件中自定义属性    }

51在用户控件中自定义属性在用户控件中自定义属性    /**//// <summary>
52在用户控件中自定义属性    /// 获取类别
53在用户控件中自定义属性    /// </summary>
54在用户控件中自定义属性    /// <param name="ClassNameStr">类名</param>
55在用户控件中自定义属性    /// <returns></returns>

56在用户控件中自定义属性    private string GetClassID(string ClassNameStr)
57在用户控件中自定义属性在用户控件中自定义属性    在用户控件中自定义属性{
58在用户控件中自定义属性        LixyClass ReadData = new LixyClass();
59在用户控件中自定义属性        DataTable Txt = ReadData.ExecuteSqlRead("select ID from TxtClass where ClassName='" + ClassNameStr + "'");
60在用户控件中自定义属性        return Txt.Rows[0]["ID"].ToString();
61在用户控件中自定义属性    }

62在用户控件中自定义属性}

63在用户控件中自定义属性


然后在ASPX页面中如何使用呢?建一个 Test.aspx ,有两种办法在页面中插入控件:
1、是在后台代码页面中用代码动态创建控件(这个我前面已经写了一篇文章)
2、是直接拖入,从 解决方案资源管理器 中用鼠标直接将ASCX文件拖入到ASPX页面

拖入后,开发工具会自动在Test.aspx中创建如下代码:

<%@ Register Src="TxtData.ascx" TagName="TxtData" TagPrefix="uc8" %>
//引入用户控件


<uc8:txtdata id="TxtData3" runat="server" TxtClass="企业信息化"></uc8:txtdata>
//创建控件实例,控件对象名为TxtData3 
//TxtClass="企业信息化"  这里就是给我们用户控件中自己定义的TxtClass属性赋值

就写这么多了,希望对入门级别的朋友有用。
 
有人问我用的什么版本,我用的VS 2005 pro,下面是用户控件TxtData.ascx拖放到form页中的切图:
在用户控件中自定义属性

至于你的为什么没有,我就不知道啦,应该有的。