如何在for循环中为textboxes添加css类

时间:2018-05-18 07:25:56

标签: c# asp.net

我有关于我对textbox字符串null的验证的问题,我确实做了一个for循环来计算文本框是空的,但我的问题是我如何使用空文本框上的红色背景添加css样式如果我正在使用一个for循环。我已经有了css样式示例:(txtNumberOfDaysCovered.cssClass =“form-control border-danger”)

我的循环:

 string[] countTextbox = new string[8];
 countTextbox[0] = txtEndDate.Text;
 countTextbox[1] = txtLessLeaveTaken.Text;
 countTextbox[2] = txtnumberOfDaysCovered.Text;
 countTextbox[3] = txtoperation.Text;
 countTextbox[4] = txtposition.Text;
 countTextbox[5] = txtStartDate.Text;
 countTextbox[6] = txtThissApp.Text;
 countTextbox[7] = txApprover.Text;

 for (int i = 0; i < countTextbox.Length; i++)
 {
     if (string.IsNullOrWhiteSpace(countTextbox[i]))
     {

         ValidationMessage.Text = "Please fill up all the required fields";
         ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", "<script>$('#modal_form_inline').modal('show');$('#modal_form_inline_Confimation').modal('hide');</script>", false);
                return;
     }

}

我不能简单地做这些ff代码:countTextbox[i].cssClass = "form-control border-danger"

3 个答案:

答案 0 :(得分:0)

  

我只是不能简单地做这些ff代码:countTextbox [i] .cssClass =   “形式控制边界危险”

string[] countTextbox = new string[8];

countTextbox是String Array。你不能为它分配cssClass。 你需要的只是一个Texbox。

答案 1 :(得分:0)

  TextBox[] countTextbox  = new TextBox[8];
        countTextbox[0] = txtEndDate;
        countTextbox[1] = txtLessLeaveTaken;
        countTextbox[2] = txtnumberOfDaysCovered;
        countTextbox[3] = txtoperation;
        countTextbox[4] = txtposition;
        countTextbox[5] = txtStartDate;
        countTextbox[6] = txtThissApp;
        countTextbox[7] = txApprover;

        for (int i = 0; i < countTextbox.Length; i++)
        {
            if (string.IsNullOrWhiteSpace(countTextbox[i].Text))
            {

                countTextbox[i].CssClass = "form-control border-danger";
                ValidationMessage.Text = "Please fill up all the required fields";
                ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", "<script>$('#modal_form_inline').modal('show');$('#modal_form_inline_Confimation').modal('hide');</script>", false);
                return;
            }


        }

解决了!!

答案 2 :(得分:0)

你可以使用Form Validations,但无论如何你想以这种方式做,你可以做下面未经测试(未编译)的事情,但你会有一个想法

var ListOfTextBoxes = new string[] { "txtEndDate","txtLessLeaveTaken","txtnumberOfDaysCovered","txtoperation","txtposition","txtStartDate","txtThissApp","txApprover"};
ListOfTextBoxes.ForEach(m => 
{
    var textbox = (TextBox)FindControl(m);
    if(string.IsNullOrWhiteSpace(textbox.text))
    {
       textbox.cssClass = "form-control border-danger";
    }
 });

查看Control.FindControl了解详情。