zoukankan      html  css  js  c++  java
  • (转)简单自定义控件

    简单的自定义控件

    分类: ASP.NET 391人阅读 评论(10) 收藏 举报

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

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5.   
    6. namespace CustomerWebControls  
    7. {  
    8.     /// <summary>   
    9.     /// 自定义的一些控件   
    10.     /// </summary>   
    11.     public class userlogin : System.Web.UI.Control  
    12.     {  
    13.         protected override void Render(System.Web.UI.HtmlTextWriter writer)  
    14.         {  
    15.             StringBuilder strb = new StringBuilder("");  
    16.             strb.Append("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/" + "/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ");  
    17.             strb.Append(" <html xmlns="http:/" + "/www.w3.org/1999/xhtml" > <head runat="server">");  
    18.             strb.Append("    <title>管理员登录登录</title>  ");  
    19.             strb.Append("       <link href="style/login/base.css" rel="stylesheet" type="text/css" />  ");  
    20.             strb.Append("       <link href="style/login/style.css" rel="stylesheet" type="text/css" />  ");  
    21.             strb.Append("       <style type="text/css" id="overridestyle">       body{ background-color:#F2F9FD;}/*#ddd*/         #box .block h2{  background-color:#2275b3;}/*#C66653,#646464*/  ");  
    22.             strb.Append("           .text_field{ height:30px; line-height:30px;  padding-left:5px;}      .left{ line-height:30px;} </style> </head>  ");  
    23.             strb.Append(" <body>");  
    24.             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"> ");  
    25.             strb.Append(" <div class="message notice"><p> 请输入正确的用户名和密码</p></div></div><form id="form1" class="form login" action="###"><div class="group wat-cf"> ");  
    26.             strb.Append(" <div class="left"><label class="label right">用户名:</label></div><div class="right"><input type="text" class="text_field"/> ");  
    27.             strb.Append(" </div></div><div class="group wat-cf"><div class="left"><label class="label right">密 码:</label></div> ");  
    28.             strb.Append(" <div class="right"><input type="password" class="text_field"/></div></div><div class="group navform wat-cf"> ");  
    29.             strb.Append(" <div class="right"><button class="button">");  
    30.             strb.Append(" <img src="images/key.png" alt="保存" />登录</button></div></div></form></div></div></div> ");  
    31.             strb.Append(" </body></html> ");  
    32.             writer.Write(strb.ToString());  
    33.   
    34.             base.Render(writer);  
    35.         }  
    36.     }  
    37. }  
    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" >
    <head runat="server">");
                strb.Append("    <title>管理员登录登录</title>
     ");
                strb.Append("       <link href="style/login/base.css" rel="stylesheet" type="text/css" />
     ");
                strb.Append("       <link href="style/login/style.css" rel="stylesheet" type="text/css" />
     ");
                strb.Append("       <style type="text/css" id="overridestyle">
          body{ background-color:#F2F9FD;}/*#ddd*/ 
           #box .block h2{  background-color:#2275b3;}/*#C66653,#646464*/ 
    ");
                strb.Append("           .text_field{ height:30px; line-height:30px;  padding-left:5px;}
         .left{ line-height:30px;}
    </style>
    </head>
     ");
                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标签,因为我们把很多东西封装到登陆框里面了。看代码如下:

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

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

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

    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenderLoginControl.aspx.cs" Inherits="MyWebSiteTest.admin.RenderLoginControl" %>  
    2. <%@ Register Assembly="CustomerWebControls" Namespace="CustomerWebControls" TagPrefix="cc1" %>  
    3. <cc1:userlogin ID="Userlogin1" runat="server"></cc1:userlogin>  
    <%@ 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方法。它是

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

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

    下面是我的评论

    引用“wanshutao”的评论:“tools工具栏打开,会看到多了一组”,这个并没有多了一组啊,是不是我记得需要在工具栏里添加啊,希...


    有时候是不会显示的,,下面2种方法试试肯定出来:1.把用户控件这一层重新生成一下,然后再打开工具栏;
    2.aspx页面切换到视图编辑状态就出来了

    Re: 黑手党维多 2013-04-25 09:33发表 [回复] [引用] [举报] [删除]
    回复Joyhen:我最后用的方法是在工具栏上添加项,然后添加的生成的dll文件,然后工具栏就有了
    5楼 黑手党维多 2013-04-23 11:11发表 [回复] [引用] [举报] [删除]
    而且删除了所有html后,只留下<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenderLoginControl.aspx.cs" Inherits="MyWebSiteTest.admin.RenderLoginControl" %> ,
    添加不进去<cc1:userlogin(就是木有智能感知),后来我试了试<html><body><cc1:userlogin</boty></html>就有智能感知了,可是,自定义控件里是整个html,如果外层再加一个,不就是两层html了吗,希望楼主看一下,谢谢
    Re: Joyhen 2013-04-25 08:21发表 [回复] [引用] [举报]
    回复wanshutao:整块的html这样子输出是不对的,我这么做事不过是起一个开头表明各种aspx控件的原理,都是尽心render渲染输出的,你看完后面就知道
  • 相关阅读:
    把git项目放到个人服务器上
    关于fcitx无法切换输入法的问题解决
    博客变迁通知
    (欧拉回路 并查集 别犯傻逼的错了) 7:欧拉回路 OpenJudge 数据结构与算法MOOC / 第七章 图 练习题(Excercise for chapter7 graphs)
    (并查集) HDU 1856 More is better
    (并查集 不太会) HDU 1272 小希的迷宫
    (并查集 注意别再犯傻逼的错了) HDU 1213 How Many Tables
    (最小生成树 Kruskal算法) 51nod 1212 无向图最小生成树
    (并查集) HDU 1232 畅通工程
    (最小生成树 Prim) HDU 1233 还是畅通工程
  • 原文地址:https://www.cnblogs.com/wanshutao/p/3632596.html
Copyright © 2011-2022 走看看