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);
- }
- }
- }
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" %>
<%@ 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>
<%@ 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
System.Web.UI.Control的一个方法,该方法将控件在客户端显示之前做一些代码的渲染。
下一节我们做更具体的处理
下面是我的评论
有时候是不会显示的,,下面2种方法试试肯定出来:1.把用户控件这一层重新生成一下,然后再打开工具栏;
2.aspx页面切换到视图编辑状态就出来了
- 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了吗,希望楼主看一下,谢谢