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))]

解决方案 »

  1.   

    另:参数的保存参见 http://dayuer.com/dayuer/posts/2554.aspx
      

  2.   

    第一个刷新问题,给你的FastLoginMode属性加上这个设计时支持属性试试:
    [NotifyParentPropertyAttribute(true)]
    public bool FastLoginMode{
    ....
    }第二个关于设计时呈现的问题,如果没有为你的控件创建控件设计器(ControlDesigner),那么在设计时vs.net会调用该控件的Render方法来呈现控件,如果你是在重写CreateChildControls方法中呈现控件,那么必须为你的控件写一个ControlDesigner,ControlDesigner有多个派生类,你可以根据你的控件所继承的父控件来选择不同的ControlDesigner。
      

  3.   

    新的问题:如何正确输出GetDesignerHtml?
    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可以访问它。
    }
    }
      

  4.   

    另外,经过试验,已经证明[NotifyParentPropertyAttribute(true)]属性不能帮助控件在设计器中改变FastLoginMode属性对显示发生影响。
      

  5.   

    到http://www.metabuilders.com/去下载一个DefaultButton控件源代码,然后注意看一下它的DefaultButtons.cs文件中的这段代码:
    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());
    }
    } }
      

  6.   

    >>如何获得userlogin.UserNameBox的设计时html输出值用你的控件中的UserName属性
      

  7.   

    为你的控件写一个控件设计器后,可以根据FastLoginMode的值来决定控件的呈现样式就行了,不需要再通知设计器刷新,它会自动刷新的。
      

  8.   

    >>用你的控件中的UserName属性这句话没看明白。我把ASP:TextBox UserNameBox公开以后Designer就可以访问了,如果要在GetDesignHtml中输出UserNameBox的外观,需要自己输出一个
    "<input type=text value="+userlogin.UserName+" id="+ userlogin.UserNameBox.ClientID+"/>"
    吗?
      

  9.   

    谢谢大家,我已经找到了原因。完整的代码如下:
    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>"));
    }
    }
    }
    }