ok,现继承一个 ControlDesigner类 public class UserLoginDesigner:ControlDesigner
{
public override string GetDesignTimeHtml()
{
UserLogin userlogin = (UserLogin)base.Component;
switch (userlogin.FastLoginMode)
{
case true:
//do......
break;
case false:
//do......
break;
}
}
}然后,为你自己的控件指定一个设计器[DesignerAttribute(typeof(UserLoginDesigner))]
{
public override string GetDesignTimeHtml()
{
UserLogin userlogin = (UserLogin)base.Component;
switch (userlogin.FastLoginMode)
{
case true:
//do......
break;
case false:
//do......
break;
}
}
}然后,为你自己的控件指定一个设计器[DesignerAttribute(typeof(UserLoginDesigner))]
[NotifyParentPropertyAttribute(true)]
public bool FastLoginMode{
....
}第二个关于设计时呈现的问题,如果没有为你的控件创建控件设计器(ControlDesigner),那么在设计时vs.net会调用该控件的Render方法来呈现控件,如果你是在重写CreateChildControls方法中呈现控件,那么必须为你的控件写一个ControlDesigner,ControlDesigner有多个派生类,你可以根据你的控件所继承的父控件来选择不同的ControlDesigner。
public class LoginModeWebControlDesigner: System.Web.UI.Design.ControlDesigner
{
public override string GetDesignTimeHtml()
{
UserLogin userlogin = (UserLogin)base.Component;
StringBuilder sb = new StringBuilder();
if (userlogin.FastLoginMode)
{
sb.Append("<table style='padding: 3px;'><tr><td>");
}
sb.Append("用户名:");
if (userlogin.FastLoginMode)
{
sb.Append("</td><td>");
}
//接下来,如何获得userlogin.UserNameBox的设计时html输出值???我已经在源代码中把UserNameBox之类的控件设置为internal,使得LoginModeWebControlDesigner可以访问它。
}
}
private void notifyDesignerOfChange() {
// Thanks to Paul Easter for this code on microsoft.public.dotnet.framework.aspnet.buildingcontrols // Tell the designer that the component has changed
if ( this.Site != null && this.Site.DesignMode ) {
try {
IDesignerHost host = (IDesignerHost)this.Site.GetService(typeof(IDesignerHost));
if ( host != null ) {
IComponentChangeService changer = (IComponentChangeService)host.GetService(typeof(IComponentChangeService));
if ( changer != null ) {
changer.OnComponentChanged(this,null,null,null);
}
}
} catch ( Exception ex ) {
System.Windows.Forms.MessageBox.Show("Problem notifying designer of change to DefaultButtons." + System.Environment.NewLine + ex.Message );
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
} }
"<input type=text value="+userlogin.UserName+" id="+ userlogin.UserNameBox.ClientID+"/>"
吗?
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;[assembly:TagPrefix("MBS.CustomControl", "mbs")]
namespace MBS.CustomControl
{
public class UserLoginDesigner: System.Web.UI.Design.ControlDesigner
{
public string GetRenderedComponentString(Control control)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
control.RenderControl(htw);
return sw.ToString();
}
public override string GetDesignTimeHtml()
{
UserLogin userlogin = (UserLogin)base.Component;
StringBuilder sb = new StringBuilder();
if (!userlogin.FastLoginMode)
{
sb.Append("<table style='padding: 3px;'><tr><td>");
}
sb.Append("用户名:");
if (!userlogin.FastLoginMode)
{
sb.Append("</td><td>");
}
sb.Append(GetRenderedComponentString(userlogin.UserNameBox));
if (!userlogin.FastLoginMode)
{
sb.Append("</td></tr><tr><td>");
}
sb.Append("口令:");
if (!userlogin.FastLoginMode)
{
sb.Append("</td><td>");
}
sb.Append(GetRenderedComponentString(userlogin.PasswordBox));
if (!userlogin.FastLoginMode)
{
sb.Append("</td></tr><tr><td colspan=2 align=center>");
}
sb.Append("<INPUT TYPE=submit name=" + userlogin.UniqueID +
" Value='登录' />");
if (!userlogin.FastLoginMode)
{
sb.Append("</td></tr></table>");
}
return sb.ToString();
}
}
/// <summary>
/// UserLogin 的摘要说明。
/// </summary>
[ToolboxData("<{0}:UserLogin runat=server />"),
DesignerAttribute(typeof(UserLoginDesigner), typeof(IDesigner)),
DefaultEvent("Login")]
public class UserLogin : Control, INamingContainer, IPostBackEventHandler
{
private Unit _BoxWidth = new Unit("120px");
private bool _FastLoginMode = false;
public UserLogin()
{
this.UserNameBox = new TextBox();
this.PasswordBox = new TextBox();
} [Category("Appearance")]
public bool FastLoginMode
{
get{ return _FastLoginMode;}
set{ _FastLoginMode = value;}
}
[Category("Appearance")]
public Unit BoxWidth
{
get
{
return _BoxWidth;
}
set
{
_BoxWidth = value;
this.UserNameBox.Width = value;
this.PasswordBox.Width = value;
}
} internal TextBox UserNameBox;
internal TextBox PasswordBox; public event EventHandler Login; public string UserName
{
get
{
return UserNameBox.Text;
}
set
{
UserNameBox.Text = value;
}
}
public string Password
{
get
{
return PasswordBox.Text;
}
}
public void DoLogin(object sender, EventArgs e)
{
if(Login != null)
{
Login(sender, e);
}
} // Define the method of IPostBackEventHandler that raises change events.
public void RaisePostBackEvent(string eventArgument)
{
this.DoLogin(this, new EventArgs());
} protected override void CreateChildControls()
{
this.Controls.Clear(); if(!FastLoginMode)
{
this.Controls.Add(new LiteralControl("<table style='padding: 3px;'><tr><td>用户名:</td><td>"));
}
else
{
this.Controls.Add(new LiteralControl("用户名:"));
}
UserNameBox.Width = BoxWidth;
UserNameBox.Text = UserName;
this.Controls.Add(UserNameBox);
if(!FastLoginMode)
{
this.Controls.Add(new LiteralControl("</td></tr><tr><td>口令:</td><td>"));
}
else
{
this.Controls.Add(new LiteralControl("口令:"));
}
PasswordBox.Width = BoxWidth;
this.Controls.Add(PasswordBox);
PasswordBox.TextMode = TextBoxMode.Password;
if(!FastLoginMode)
{
this.Controls.Add(new LiteralControl("</td></tr><tr><td colspan=2 align=center>"));
}
this.Controls.Add(new LiteralControl("<INPUT TYPE=submit name=" + this.UniqueID +
" Value='登录' />"));
if(!FastLoginMode)
{
this.Controls.Add(new LiteralControl("</td></tr></table>"));
}
}
}
}