C# 如何在文本框的for循环中添加css类

C# 如何在文本框的for循环中添加css类,c#,asp.net,C#,Asp.net,关于文本框字符串null的验证,我有一个问题,我做了一个for循环来计算空文本框,但我的问题是,如果我使用for循环,我如何在空文本框上添加一个红色背景的css样式。我已经有了css样式的示例:(txtNumberOfDaysCovered.cssClass=“表单控制边界危险”) 我的for循环: string[] countTextbox = new string[8]; countTextbox[0] = txtEndDate.Text; countTextbox[1] = txtL

关于文本框字符串null的验证,我有一个问题,我做了一个for循环来计算空文本框,但我的问题是,如果我使用for循环,我如何在空文本框上添加一个红色背景的css样式。我已经有了css样式的示例:(txtNumberOfDaysCovered.cssClass=“表单控制边界危险”)

我的for循环:

 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;
     }

}
string[]countTextbox=新字符串[8];
countTextbox[0]=txtEndDate.Text;
countTextbox[1]=TXTLESLEVETAKEN.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;
对于(int i=0;i
我只是不能简单地做这些ff代码:
countTextbox[i].cssClass=“form control border danger”

我只是不能简单地做这些ff代码:countTextbox[i].cssClass= “表格管制边境危险”

countTextbox
是字符串数组。您不能将cssClass分配给它。 你所需要的只是一个文本框。

TextBox[]countTextbox=newtextbox[8];
  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;
            }


        }
countTextbox[0]=txtEndDate; countTextbox[1]=TXTLESLEVETAKEN; countTextbox[2]=txtnumberOfDaysCovered; countTextbox[3]=txtoperation; countTextbox[4]=txtposition; countTextbox[5]=txtStartDate; countTextbox[6]=txtThissApp; countTextbox[7]=txApprover; 对于(int i=0;i

解决了

您可以使用
表单验证
来实现这一点,但无论您以何种方式希望这样做,您也可以执行类似于以下未测试(未编译)的操作,但您会有一个想法

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";
    }
 });

查看更多信息。

您知道表单验证已经存在,对吗?谢谢各位。我将添加以下代码作为注释,以使我变得更好,我非常感谢:)
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";
    }
 });