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

|M| 第四贴:上星期五幕白兄写的一个正则和不为空控件合并的,在cs中可以,我写到控件中就有个问题 大家来看看
http://community.csdn.net/Expert/topic/5432/5432250.xml?temp=.512768
http://community.csdn.net/Expert/topic/5432/5432306.xml?temp=.6382715
http://community.csdn.net/Expert/topic/5436/5436352.xml?temp=5.195254E-02

RequiredFieldValidator   req   =   new   RequiredFieldValidator();
                        req.ControlToValidate   =   this.ControlToValidate;
                        req.ErrorMessage   =   this.NotNullMessage;
                        req.Display   =   this.Display;
                        Literal   l   =   new   Literal();
                        l.Text   =   this.ErrorMessage;
                        this.Controls.Add(l);
                        this.Controls.Add(req);

<cc1:webRev   ID= "WebRev1 "   runat= "server "   Display= "Dynamic "     ControlToValidate= "TextBox1 "   ErrorMessage= "数字 "   NotNullMessage= "不可空 "   ValidationExpression= "\d* "> </cc1:webRev>

因为加多了一个Display= "Dynamic "   就不显示了
但这个对我来说是很重要的

因为加多了   Display= "Dynamic "   的时候
当TextBox1为空的时候并没有验证WebRev1
这时WebRev1的Display=none
虽然这个时候验证了里面的RequiredFieldValidator控件,他的Display= " "
但是因为他包含在WebRev1中当WebRev1不显示的时候他自然就显示不出来了

------解决方案--------------------
bang ding
------解决方案--------------------
using System; using System.Configuration; 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; namespace AA { /// <summary> /// MyRegValidtor 的摘要说明 /// </summary> public class MyRegValidtor : System.Web.UI.WebControls.WebControl { public MyRegValidtor() { // // TODO: 在此处添加构造函数逻辑 // } protected override void OnInit(EventArgs e) { } protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); } protected override void OnPreRender(EventArgs e) { RequiredFieldValidator req = new RequiredFieldValidator(); req.ControlToValidate = this.ControlToValidate; req.ErrorMessage = this.NotNullMessage; req.Display = this.Display; this.Controls.Add(req); RegularExpressionValidator reg = new RegularExpressionValidator(); reg.ControlToValidate = this.ControlToValidate; reg.ErrorMessage = this.ErrorMessage; reg.ValidationExpression = this.ValidationExpression; reg.Display = this.Display; this.Controls.Add(reg); } public string ErrorMessage { get { return ViewState[ "ErrorMessage "] != null ? (string)ViewState[ "ErrorMessage "] : " "; } set { ViewState[ "ErrorMessage "] = value;