简单的自定义控件

1.建一个类库,名字叫CustomerWebControls,添加一个userlogin.cs类,注意,userlogin类需要继承 System.Web.UI.Control代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomerWebControls
{
    /// <summary>
    /// 自定义的一些控件
    /// </summary>
    public class userlogin : System.Web.UI.Control
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            StringBuilder strb = new StringBuilder("");
            strb.Append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http:/" + "/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
            strb.Append(" <html xmlns=\"http:/" + "/www.w3.org/1999/xhtml\" >\r\n<head runat=\"server\">");
            strb.Append("    <title>管理员登录登录</title>\r\n ");
            strb.Append("       <link href=\"style/login/base.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n ");
            strb.Append("       <link href=\"style/login/style.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n ");
            strb.Append("       <style type=\"text/css\" id=\"overridestyle\">\r\n      body{ background-color:#F2F9FD;}/*#ddd*/ \r\n       #box .block h2{  background-color:#2275b3;}/*#C66653,#646464*/ \r\n");
            strb.Append("           .text_field{ height:30px; line-height:30px;  padding-left:5px;}\r\n     .left{ line-height:30px;}\r\n</style>\r\n</head>\r\n ");
            strb.Append(" <body>");
            strb.Append(" <div id=\"box\"><div style=\"height:129px;\"></div><div class=\"block\" id=\"block-login\"><h2>管理员登录</h2><div class=\"content login\"><div class=\"flash\"> ");
            strb.Append(" <div class=\"message notice\"><p> 请输入正确的用户名和密码</p></div></div><form id=\"form1\" class=\"form login\" action=\"###\"><div class=\"group wat-cf\"> ");
            strb.Append(" <div class=\"left\"><label class=\"label right\">用户名:</label></div><div class=\"right\"><input type=\"text\" class=\"text_field\"/> ");
            strb.Append(" </div></div><div class=\"group wat-cf\"><div class=\"left\"><label class=\"label right\">密 码:</label></div> ");
            strb.Append(" <div class=\"right\"><input type=\"password\" class=\"text_field\"/></div></div><div class=\"group navform wat-cf\"> ");
            strb.Append(" <div class=\"right\"><button class=\"button\">");
            strb.Append(" <img src=\"images/key.png\" alt=\"保存\" />登录</button></div></div></form></div></div></div> ");
            strb.Append(" </body></html> ");
            writer.Write(strb.ToString());

            base.Render(writer);
        }
    }
}

编译一下,就会在bin目录多一个dll文件,不管它,我们先在同目录的解决方案里面建一个网站MyWebSiteTest

然后引用CustomerWebControls层。好了新建一个页面,这个页面秃溜溜的,就一个页面page标签,因为我们把很多东西封装到登陆框里面了。看代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenderLoginControl.aspx.cs" Inherits="MyWebSiteTest.admin.RenderLoginControl" %>

这个时候把tools工具栏打开,会看到多了一组


我们把它直接拖到页面上去,这时页面代码就成了

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenderLoginControl.aspx.cs" Inherits="MyWebSiteTest.admin.RenderLoginControl" %>
<%@ Register Assembly="CustomerWebControls" Namespace="CustomerWebControls" TagPrefix="cc1" %>
<cc1:userlogin ID="Userlogin1" runat="server"></cc1:userlogin>

把网站生成以下,浏览该页面看看效果


这只是一个简单的例子,这里需要晓得一点。自定义控件一般要重写Render方法。它是

System.Web.UI.Control
的一个方法,该方法将控件在客户端显示之前做一些代码的渲染。

下一节我们做更具体的处理

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值