如何动态验证创建的控制? [英] How to validate dynamically created control?
问题描述
我有一个asp.net页面,它的一些控制是动态创建的,这些控件是以下中的一个;文本框,日历,或DropDownList的。
在某些情况下,这些控制,应根据标志从数据库读取验证?
有什么办法来验证 动态创建控件?
我得到了该问题的解决方案。 其中一个是我和这个页面,它的Ajax功能的主要问题,我需要验证动态创建的控件。
我的解决方案,并能正常工作,同时创造了控制,我添加了一个输入属性,其市场上赢得它,因为它需要与否,而且标志着其另一属性,因为它是一个字段被验证或不?
使用JavaScript,我通过所有的输入标签与属性的动态控制,并根据验证attribut,我确认这一点。简单吧?
样品code: 而对照创建,标志着它像下面的
txtBox.Attributes.Add(类型,T); //控制类型。
txtBox.Attributes.Add(IsKeyField,Y); //动态创建领域。
txtBox.Attributes.Add(IsMandatory,Y); //是强制性的还是不?
JavaScript的code
VAR inputControls = document.getElementsByTagName(输入);
对于(VAR I = 0; I< inputControls.length;我++)
{
如果(inputControls [I] .getAttribute(IsKeyField)==Y)
{
如果(inputControls [I] .getAttribute(类型)==T||(inputControls [I] .getAttribute(类型)==C))
{
如果(inputControls [I] .getAttribute(IsMandatory)==Y)
{
如果(inputControls [I] .value的==)
{
ERRORMSG + =\ N+ inputControls [I] .getAttribute(KeyField_Name)+是必需的。
isValidated = FALSE;
}
}
}
}
}
当然,你可以调用code点击所需要的按钮时。
btnUpload.Attributes.Add(onclick事件,JavaScript的:如果(ValidateMandatoryFields())返回false;!);
I have an asp.net page, some of its controls are created dynamically, these controls are one of the following; textbox, calendar, or dropdownlist.
These controls in some cases, should be validated based on flag read from db?
Is there any way to validate dynamically created controls?
I got a solution for that problem. One of the main problems that I had with this page that it's ajax-enabled and I need to validate dynamically created controls.
My solution and it works properly, while creating the Control, I added an input attribute to it markes it as it is required or not, and another attribute that marks it as it is a field to be validated or not?
Using Javascript, I go through all input tags with attribute "dynamic control" and based on "to validate attribut", I validate it or not. Simple, right?
Sample Code: While control creating, mark it like the following
txtBox.Attributes.Add("Type", "T"); // Type of control.
txtBox.Attributes.Add("IsKeyField", "Y"); // Is dynamically created field.
txtBox.Attributes.Add("IsMandatory", "Y"); // Is mandatory or not?
JavaScript code
var inputControls = document.getElementsByTagName("input");
for(var i=0 ; i<inputControls.length ; i++)
{
if ( inputControls[i].getAttribute("IsKeyField") == "Y" )
{
if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C"))
{
if(inputControls[i].getAttribute("IsMandatory") == "Y")
{
if(inputControls[i].value == "")
{
errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
isValidated = false;
}
}
}
}
}
Of course, you can call that code while clicking the required button.
btnUpload.Attributes.Add("onClick", "javascript:if(!ValidateMandatoryFields()) return false;");
这篇关于如何动态验证创建的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!