如何添加2个文本框的值并检查它是否等于第三个?

时间:2016-03-25 07:21:21

标签: c# asp.net customvalidator

我有一个表单,其中包含多个 private static DataTable ConvertToDataTable(IEnumerable<T> enumerable) { var properties = TypeDescriptor.GetProperties(typeof(T)); var table = new DataTable(); foreach (PropertyDescriptor prop in properties) table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); foreach (var item in enumerable) { var row = table.NewRow(); foreach (PropertyDescriptor prop in properties) row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; table.Rows.Add(row); } return table; } textboxes菜单。

我想计算并检查这些dropdown的总和是否等于第三,每个文本框都有一个textboxes菜单,该下拉菜单的值为+1或-1 。例如,我有以下控件:

dropdown

我想检查是否:

 txt1 with ddl1; txt2 with ddl2; txt3 with ddl3; txt4 with ddl4;

此外,我正在使用自定义验证器,并希望在其中检查此条件。

3 个答案:

答案 0 :(得分:0)

  function Total() {
            var sumofall;            
            var FirstText = document.getElementById('txt1').value;
            var SecondText = document.getElementById('txt2').value;
            var ThirdText = document.getElementById('txt3').value;
            var FouthText = document.getElementById('txt4').value;
            var dropdown = document.getElementById("ddl1");
            var dropdown2 = document.getElementById("ddl2");
            var dropdown3 = document.getElementById("ddl3");
            var dropdown4 = document.getElementById("ddl4");
            var value = dropdown.options[dropdown.selectedIndex].value;
            var value2 = dropdown2.options[dropdown2.selectedIndex].value;
            var value3 = dropdown3.options[dropdown3.selectedIndex].value;
            var value4 = dropdown4.options[dropdown4.selectedIndex].value;
            var text = dropdown.options[dropdown.selectedIndex].text;
            var text2 = dropdown2.options[dropdown2.selectedIndex].text;
            var text3 = dropdown3.options[dropdown3.selectedIndex].text;
            var text4 = dropdown4.options[dropdown4.selectedIndex].text;

            if (FirstText == "")
                FirstText = 0;
            if (dropdown == "")
                dropdown = 0;

            var result = parseInt(FirstText) * parseInt(text);
            if (!isNaN(result)) {

            }
            if (SecondText == "")
                SecondText = 0;
            if (dropdown2 == "")
                dropdown2 = 0;

            if (ThirdText == "")
                ThirdText = 0;
            if (dropdown3 == "")
                dropdown3 = 0;

            if (FouthText == "")
                FouthText = 0;
            if (dropdown4 == "")
                dropdown4 = 0;

            var result2 = parseInt(SecondText) * parseInt(text2);
            var result3 = parseInt(ThirdText) * parseInt(text3);
            var result4 = parseInt(FouthText) * parseInt(text4);

            if (!isNaN(result2) && !isNaN(result3) && !isNaN(result4)) {
                sumofall = result2 + result3 + result4;
            }


            if (result == sumofall)
                alert('Something');
            else
                alert('Error..Not Matched');
                return false;
    }

答案 1 :(得分:0)

在自定义验证器的服务器功能中,您可以写:

int firstValue = 0;
if(int32.TryParse(txt1.Text), out firstValue)
{
    int secondValue = 0;
    if(int32.TryParse(txt2.Text), out secondValue)
    {
         // ...
         // check other value, then
         arg.IsValid = firstValue * Convert.ToInt32(ddl1.SelectedValue) == 
                  (secondValue * Convert.ToInt32(ddl2.SelectedValue)) +
                  (thirdValue * Convert.ToInt32(ddl3.SelectedValue)) +
                  (fourthValue * Convert.ToInt32(ddl4.SelectedValue));

    }
    else
         lblEsito = "Il valore nel secondo TextBox non è un intero";
}
else
    lblEsito = "Il valore nel primo TextBox non è un intero";

我认为下拉列表的值是整数,并且下拉列表控件中存在值为“0”或“”的任何“Select”。 下次你必须发布一些代码。

答案 2 :(得分:0)

希望您首先需要将textbox和ddl值更改为整数。

int txt1 = Convert.ToInt32(TextBox1.Text);             int dd1 = Convert.ToInt32(DropDownList1.SelectedValue);

        int txt2 = Convert.ToInt32(TextBox2.Text);
        int dd2 = Convert.ToInt32(DropDownList2.SelectedValue);

        int txt3 = Convert.ToInt32(TextBox3.Text);
        int dd3 = Convert.ToInt32(DropDownList3.SelectedValue);


        if (txt1 * dd1 == (txt2 * dd2) + (txt3 * dd3))
        {
            //YOur code here
        }