虽然动态文本框为空,但不会弹出警报消息

时间:2011-04-18 17:23:30

标签: asp.net

以下是我如何创建动态文本框的代码。我想验证创建的任何动态文本框是否为空,如果是,则应向用户提示错误消息或警告,提示填写Additem按钮单击上的文本框。我收到输入到该功能的警报消息,之后我没有收到任何提示用户填写文本框“xxxx”的消息。

请帮我修复代码

 if (ControlType == "TextBox")
        {
            int textBoxLength;
            TextBox MynewTextBox = new TextBox();
            MynewTextBox.ID = "txt" + Fldname;

            MynewTextBox.Width = 100;
            MynewTextBox.BorderStyle = BorderStyle.Solid;
            MynewTextBox.Attributes.Add("Type", "T");
            MynewTextBox.Attributes.Add("IsKeyField", "Y");
            MynewTextBox.Attributes.Add("IsMandatory", "Y");  
        }



protected void Page_Load(object sender, EventArgs e)
    {    
     btnAddItem.Attributes.Add("onClick", "if(!ValidateMandatoryFields()) return false;"); 
    }
  <script language="javascript" type="text/javascript">

function ValidateMandatoryFields() {
alert("entered into the function");
var inputControls = document.getElementsByTagName("input");
alert(inputControls.length);
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 == "") {
                    alert(inputControls[i].getAttribute("KeyField_Name") + "is required.");
                    errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
                    isValidated = false;
                }
            }
        }
    }
}
  }
    }
  </script>

enter link description here
 我按照上面的链接

它仍然没有帮助我

function ValidateMandatoryFields() {
alert("entered into the function");
var inputControls = document.getElementsByTagName("input");
alert(inputControls.length);
for (var i = 0; i < inputControls.length; i++) {

    if (inputControls[i].getAttribute("IsKeyField") == "Y") {
        alert(inputControls[i]);
    }
    else {
        alert("first if statemenet");
    }

    if (inputControls[i].getAttribute("Type") == "T" || (inputControls[i].getAttribute("Type") == "C")) {
        alert(inputControls[i]);
    }
    else {
        alert("second if statement");
    }
    if (inputControls[i].getAttribute("IsMandatory") == "Y") {
        alert(inputControls[i]);
    }
    else {
        alert("third if statement");
    }

    if (inputControls[i].value == "") {
        alert(inputControls[i].getAttribute("KeyField_Name") + "is required.");--error here
        errorMsg += "\n" + inputControls[i].getAttribute("KeyField_Name") + " is required.";
        isValidated = false;
    }
    else {
        alert("name: " + inputControls[i].id);
        alert("length: " + inputControls[i].value.length());
        alert("value: " + inputControls[i].value);
    }
}

}

出现所有其他语句的弹出窗口,最后我收到一条错误,指出Microsoft JScript运行时错误:预期的对象。这意味着我的if条件都不是真的......我如何让它工作......请帮助我正确的逻辑

2 个答案:

答案 0 :(得分:0)

尝试添加else子句以查看值是什么:

if (inputControls[i].value == "") {
   ... your code
} else
{
   alert("name: " + inputControls[i].id);
   alert("length: " + inputControls[i].value.length());
   alert("value: " + inputControls[i].value);
}

它们可能设置为null或空格而不是空字符串。

答案 1 :(得分:0)

我能够修复我的代码,如下所示:

function validateInput() {
var arrTextBox = document.getElementsByTagName("input");
var retValue = 1;
var returnValue = 1;
for (i = 0; i < arrTextBox.length; i++) {
    if (arrTextBox[i].type =="text" && arrTextBox[i].getAttribute("IsMandatory")=="Y" && arrTextBox[i].value == "") {
        retValue = 0;
    }
}
if (retValue == 0) {
    alert("Validation Failed");
    return false;
}
else {
    alert("Validation Success");
    return true;
}
在ascx.cs文件中

 btnAddItem.Attributes.Add("onclick", "{return validateInput()};");