如何使用包含验证控件的asp.net创建Web服务器控件 [英] how to create web server control using asp.net that contain validation control

查看:80
本文介绍了如何使用包含验证控件的asp.net创建Web服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我想使用包含带有RequiredFieldValidator控件的TextBox的asp.net进行Web服务器控制。我试过但验证控制不起作用。



请提前帮助我。

Dear All ,
I want to web server control using asp.net that contain TextBox with RequiredFieldValidator controls. i have tried but validation control not work.

please help me thanks in advance.

protected override void RenderContents(HtmlTextWriter output)
        {
            Panel mypanel = new Panel();
            TextBox txtName = new TextBox();
            txtName.ID = "txtNameVal";
            txtName.EnableViewState = true;

            RequiredFieldValidator myrqv = new RequiredFieldValidator();
            myrqv.ErrorMessage = "Please enter the value";
            myrqv.EnableViewState = true;
            myrqv.EnableTheming = true;
            myrqv.ClientIDMode = System.Web.UI.ClientIDMode.Inherit;
            myrqv.Display = ValidatorDisplay.Dynamic;
            myrqv.ValidateRequestMode = System.Web.UI.ValidateRequestMode.Inherit;
            myrqv.ViewStateMode = System.Web.UI.ViewStateMode.Inherit;

            myrqv.ControlToValidate = txtName.ID;
            //myrqv.Text = "*";
            txtName.AutoPostBack = false;
            mypanel.Controls.Add(txtName);
            mypanel.Controls.Add(myrqv);


            mypanel.RenderControl(output);

            output.Write("<br /><br />");
            string mytxt = "<br />";
            output.Write(mytxt);
            //output.Write(Text);
        }



vilas jadhav


vilas jadhav

推荐答案

试试这篇文章,因为这可以帮助你解决问题:

构建ASP.NET自定义Web控件:a一个文本框和验证器 [ ^ ]
Try this article as this might help you solving the problem:
Building an ASP.NET custom web control: a textbox and validator in one[^]






我已经解决了这个问题问题,它与Rangevalidation,RegularExpressionValidator和RequiredFieldValidator等验证控件正常工作



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.IO;

使用System.Linq;

u使用System.Threading.Tasks演唱System.Text;

;使用System.Web获得
;使用System.Web.UI获取
;

使用System.Web.UI.WebControls;



命名空间IFS2.BackOffice.Web.Common

{

[DefaultProperty(Text)]

[ToolboxData(< {0}:RequiredTextBox1 runat = server>)]

公共类RequiredTextBox1:TextBox

{

private RequiredFieldValidator req;

private RegularExpressionValidator reqExpress;

private RangeValidator RangeVldtr;

公共字符串InvalidMessage;

公共字符串ClientScript =true;



[Bindable(true)]

[类别(外观)]

[DefaultValue()]

[Localizable(true)]

公共字符串RequiredErrMsg

{

get

{

String s =(String)ViewState [RequiredErrMsg];

return((s == null)? 这个领域是强制性的:s);

}



set

{

ViewState [RequiredErrMsg] = value;

}

}



public enum YesNo: int

{

True = 0,

False = 1

}



[Bindable(true)]

[类别(外观)]

[DefaultValue()]

[Localizable(true)]

[EditorBrowsable(EditorBrowsableState.Always)]

public bool RequiredField

{

get

{

object o = ViewState [RequiredField];

if(o == null)

返回false;

返回(bool)o;

}



设置

{

ViewState [RequiredField] = value;

}

}





[Bindable(true)]

[类别(外观)]

[ DefaultValue()]

[Localizable(true)]

[EditorBrowsable(EditorBrowsableState.Always)]

public bool RangevalidationField

{

get

{

object o = ViewState [RangevalidationField];

if(o == null)

返回false;

return(bool)o;

}



set

{

ViewState [RangevalidationField] = value;

}

}



[Bindable(true)]

[类别( 外观)]

[DefaultValue()]

[Localizable(true)]

[EditorBrowsable(EditorBrowsableState.Always)]

公共字符串RangevalidationMaxVal

{

get

{

string o =( string)ViewState [RangevalidationMaxVal];

if(o == null)

return string.Empty;

return o;

}



set

{

ViewState [RangevalidationMaxVal] = value;

}

}



[Bindable(true)]

[类别(外观)]

[DefaultValue()]

[Localizable(true)]

[EditorBrowsable(EditorBrowsableState.Always) ]

公共字符串RangevalidationMinVal

{

get

{

string o =(string)ViewState [RangevalidationMinVal];

如果(o == null)

返回string.Empty;

返回o;

}



set

{

ViewState [RangevalidationMinVal] = value;

}

}



[Bindable(true)]

[类别(外观)]

[DefaultValue()]

[Localizable(true)]

[EditorBrowsable(EditorBrowsableState.Always)]

公共字符串FieldLength

{

get

{

string o =(string)ViewState [FieldLength];

if(o == null)

返回字符串。空;

返回o;

}



设定

{

ViewState [FieldLength] =值;

}

}



< br $> b $ b

[Bindable(true)]

[类别(外观)]

[DefaultValue( )]

[Localizable(true)]

[EditorBrowsable(EditorBrowsableState.Always)]

公共字符串RangevalidationType

{

get

{

string o =(string)ViewState [RangevalidationType];

if (o == null)

返回string.Empty;

返回o;

}



set

{

ViewState [RangevalidationType] = value;

}

}





// [Bindable(true)]

// [类别(外观)]

// [DefaultValue()]

// [Localizable(true)]

//公共字符串ID {

//获取

// {

// String s =(String)ViewState [ID];

//返回((s == null)? ClientID.ToString():s);

//}

//设置

// {

// ViewState [ID] =值;

//}



//}





protected override void OnInit(EventArgs e)

{

if(Context!= null)

{



req = new RequiredFieldValidator();



if(this.ID == null)

{

this.ID =RequiredTextBox1+ GetNumbers(ClientID).ToString();

}



RangeVldtr = new RangeValidator();

reqExpress = new RegularExpressionValidator();



//reqExpress.ValidationExpression=^ [0-9] *
Hi,

I have solve this problem and it is working properly with validation controls like Rangevalidation, RegularExpressionValidator and RequiredFieldValidator

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IFS2.BackOffice.Web.Common
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:RequiredTextBox1 runat=server>")]
public class RequiredTextBox1 : TextBox
{
private RequiredFieldValidator req;
private RegularExpressionValidator reqExpress;
private RangeValidator RangeVldtr;
public string InvalidMessage;
public string ClientScript = "true";

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string RequiredErrMsg
{
get
{
String s = (String)ViewState["RequiredErrMsg"];
return ((s == null) ? "This Field is Mandatory " : s);
}

set
{
ViewState["RequiredErrMsg"] = value;
}
}

public enum YesNo : int
{
True=0,
False=1
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public bool RequiredField
{
get
{
object o = ViewState["RequiredField"];
if (o == null)
return false;
return (bool)o;
}

set
{
ViewState["RequiredField"] = value;
}
}


[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public bool RangevalidationField
{
get
{
object o = ViewState["RangevalidationField"];
if (o == null)
return false;
return (bool)o;
}

set
{
ViewState["RangevalidationField"] = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public string RangevalidationMaxVal
{
get
{
string o = (string) ViewState["RangevalidationMaxVal"];
if (o == null)
return string.Empty;
return o;
}

set
{
ViewState["RangevalidationMaxVal"] = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public string RangevalidationMinVal
{
get
{
string o = (string)ViewState["RangevalidationMinVal"];
if (o == null)
return string.Empty;
return o;
}

set
{
ViewState["RangevalidationMinVal"] = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public string FieldLength
{
get
{
string o = (string)ViewState["FieldLength"];
if (o == null)
return string.Empty;
return o;
}

set
{
ViewState["FieldLength"] = value;
}
}



[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public string RangevalidationType
{
get
{
string o = (string)ViewState["RangevalidationType"];
if (o == null)
return string.Empty;
return o;
}

set
{
ViewState["RangevalidationType"] = value;
}
}


//[Bindable(true)]
//[Category("Appearance")]
//[DefaultValue("")]
//[Localizable(true)]
//public String ID {
// get
// {
// String s = (String)ViewState["ID"];
// return ((s == null) ? ClientID.ToString() : s);
// }
// set
// {
// ViewState["ID"] = value;
// }

//}


protected override void OnInit(EventArgs e)
{
if (Context != null)
{

req = new RequiredFieldValidator();

if (this.ID == null)
{
this.ID = "RequiredTextBox1" + GetNumbers(ClientID).ToString();
}

RangeVldtr = new RangeValidator();
reqExpress = new RegularExpressionValidator();

//reqExpress.ValidationExpression= "^[0-9]*


; //仅适用于数字而不是字母和空格

//reqExpress.ValidationExpression =^ [a-zA-Z'' - '\\\\] {1,40}
"; //only for Number not letter and space
//reqExpress.ValidationExpression = "^[a-zA-Z''-'\\s]{1,40}


这篇关于如何使用包含验证控件的asp.net创建Web服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆