日期:2014-05-19  浏览次数:20517 次

|M| 学习写控件第十二贴:如何设计控件在设计页里显示的值的设计
就像VS控件一样拖一个Label到页面的时候会默让显示Text如果Text没值时显示ID
然后我的控件我想让他默认显示MyMessage
要怎么做
就像VS控件一样拖一个Label到页面的时候会默让显示Text如果Text没值时显示ID

环境:
我用幕白兄给的
控件继承:System.Web.UI.WebControls.WebControl
已经注时显示的值字段[DefaultProperty( "MyMessage "),   ToolboxData( " <{0}:WebRev   runat=server   /> ")]
然后在
protected   override   void   OnInit(EventArgs   e)
{
        req   =   new   RequiredFieldValidator();
        req.ControlToValidate   =   this.ControlToValidate;
        req.ErrorMessage   =   this.NotNullMessage;
        req.Display   =   this.Display;
        req.ForeColor   =   this.NotNullColor;
        req.ValidationGroup   =   ValidationGroup;
        if   (FieldValidator   ==   true)   this.Controls.Add(req);


        RegularExpressionValidator   reg   =   new   RegularExpressionValidator();
        reg.ControlToValidate   =   this.ControlToValidate;
        reg.ErrorMessage   =   this.ErrorMessage;
        reg.ValidationExpression   =   this.ValidationExpression;
        reg.Display   =   this.Display;
        reg.ForeColor   =   this.ErrorColor;
        reg.ValidationGroup   =   ValidationGroup;
        this.Controls.Add(reg);

        base.OnInit(e);
}
这里添加两个验证控件
但当我把这个新的控件拖到设计页面中的时候
显示的不是我那个MyMessage
而是req.ErrorMessage   +   regErrorMessage
如上面两个的值为 "* "
那么我的控件就显示为 "** "
但我要的是显示我自定的属性 "MyMessage "   而不是那两个我添加进去的控件的ErrorMessage

谢谢


------解决方案--------------------
做一个design;

public class ValidatorDesign : System.Web.UI.Design.ControlDesigner {

public override string GetDesignTimeHtml()
{
YourControl con = (YourControl)Component;
string str = " <div ...> " + con.MyMessage + " </div> ";
return str;
}
}

(2) 然后:
[
Designer(typeof(ValidatorDesign ), typeof(IDesigner)),
]
public class YourControl :System.Web.UI.WebControls.WebControl,INamingContainer
{ ...}
------解决方案--------------------

------解决方案--------------------
protected override void RenderChildren(HtmlTextWriter writer)
{
WebControl c = (WebControl)this.Controls[0];
System.IO.StringWriter sw = new System.IO.StringWriter();

HtmlTextWriter wt = new HtmlTextWriter(sw);

c.RenderControl(wt);

writer.Write(sw.ToString().Replace( "display:none ", " "));
wt.Close();
sw.Close();


c = (WebControl)this.Controls[1];
c.RenderControl(writer);

}


------解决方案--------------------
发错了.