控件user.ascx中的代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="user.ascx.cs" Inherits="user" %>
<asp:Panel ID="Panel1" runat="server" Height="206px" HorizontalAlign="Center" Width="284px">
<br />
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="username" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="password" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Cancel" OnClick="Button2_Click" />
<br />
<br />
<asp:Label ID="message" runat="server" Text="Label"></asp:Label></asp:Panel>
这是user.ascx.cs中的源码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Util;
public partial class user : System.Web.UI.UserControl
{
//private string color;
public String UserName
{
get
{
return username.Text;
}
set
{
username.Text = value;
}
}
public String PassWord
{
get
{
return password.Text;
}
set
{
password.Text = value;
}
}
public string Color
{
get
{
return Convert.ToString(message.ControlStyle.BackColor);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
message.Text = "<b>UserName is:</b>" + this.username.Text;
message.Text+= "<b>PassWord is:</b>" + this.password.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
username.Text = "";
password.Text = "";
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
这是testusercontrol.aspx中的源码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testusercontrol.aspx.cs" Inherits="testusercontrol" %>
<%@ Register Src="user.ascx" TagName="user" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:user ID="User1" runat="server" />
</div>
</form>
</body>
</html>
就这么自制控件比较简单,不过这里还有两个问题相请教一下大家:
第一就是user.ascx.cs代码中有一个color的属性没能实现,因为我现在找不到合适简单的办法,把Sting 类型转换成color类型,希望大家指点一下
第二个就是如何将这个自制控件加到vs 2005的工具箱中像他自己所带的控件一样自由拖到窗体上,这里我是直接从解决方案管理器里直接拖在设计窗口上的,
望大家提出些意见...,Thanks!