当前位置: 代码迷 >> ASP.NET >> |M| 第四贴:上星期五幕白兄写的一个正则和不为空控件合并的,在cs中可以,小弟我写到控件中就有个有关问题 大家来看看
  详细解决方案

|M| 第四贴:上星期五幕白兄写的一个正则和不为空控件合并的,在cs中可以,小弟我写到控件中就有个有关问题 大家来看看

热度:1886   发布时间:2013-02-26 00:00:00.0
|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; } } public string ValidationExpression { get { return ViewState[ "ValidationExpression "] != null ? (string)ViewState[ "ValidationExpression "] : " "; } set { ViewState[ "ValidationExpression "] = value; } } public string ControlToValidate { get { return ViewState[ "ControlToValidate "] != null ? (string)ViewState[ "ControlToValidate "] : " "; } set { ViewState[ "ControlToValidate "] = value; } } public ValidatorDisplay Display { get { return ViewState[ "Display "] != null ? (ValidatorDisplay)ViewState[ "Display "] : ValidatorDisplay.None; } set { ViewState[ "Display "] = value; } } public string NotNullMessage { get { return ViewState[ "NotNullMessage "] != null ? (string)ViewState[ "NotNullMessage "] : " "; } set { ViewState[ "NotNullMessage "] = value; } } }}
  相关解决方案