码迷,mamicode.com
首页 > Web开发 > 详细

asp.net服务器控件开发系列一

时间:2014-11-15 00:00:18      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   sp   for   文件   

最近想写写博客记录下自己学习开发服务器控件。

第一步:搭建环境。

1、新建一个项目类库,用于保存控件;

2、新建一个Web工程,用于调用控件;

如图:

bubuko.com,布布扣

第二步:在控件类库下,新建一个服务器控件类TextBox.cs文件。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JHSoft.UI.Web.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
    public class TextBox : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
    }
}

修改方法:RenderContents

  protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write("<input type=‘text‘ value=‘" + Text + "‘/>");
        }

第三步:

1、引用控件类库。

bubuko.com,布布扣

2、在Webconfig中注册控件前缀

bubuko.com,布布扣

3、在Web工程中,新建一个页面TextBox.aspx,调用新建的服务器控件<c:TextBox>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextBox.aspx.cs" Inherits="JHSoft.SYS.Web.TextBox" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <c:TextBox runat="server" Text="Test"></c:TextBox>

    </form>
</body>
</html>

需要说明的是:

此处的控件 <c:TextBox runat="server" Text="Test"></c:TextBox>,执行完成后,生成的html代码是:

TextBox.cs类中的

protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<input type=‘text‘ value=‘" + Text + "‘/>");   //生成的普通控件,Text为显示的值
}

 

asp.net服务器控件开发系列一

标签:style   blog   http   io   color   ar   sp   for   文件   

原文地址:http://www.cnblogs.com/renzaijianghu/p/4098350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!