1
foreach (Control ctrl in Page.Controls) 
    {
        if (ctrl is TextBox)
        {
            if (((TextBox)(ctrl)).Text == "")
            {  
               helpCalss.MessageBox("Please fill the empty fields", this);
                    return;  
            }  
        }  
    }  

asp.netを使用していて、texboxを含む挿入ページがあり、ページ内のtexboxが空かどうかを確認する必要があります。空の場合は、空のテキストボックスを含むメッセージボックスを表示する必要があります。

4

3 に答える 3

0

この再帰関数を試して、ページ上のすべてのテキストボックスを取得できると思います。

    /// <summary>
    /// Find TextBoxes Recursively in the User control's control collection
    /// </summary>
    /// <param name="controls"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    private void FindControlRecursiveByType(ControlCollection controls, ref List<TextBox> OutputList)
    {
        foreach (WebControl control in controls.OfType<WebControl>())
        {
            if (control is TextBox)
                OutputList.Add(control as TextBox);
            if (control.Controls.Count > 0)
                FindControlRecursiveByType(control.Controls, ref OutputList);
        }
    }

OutputList にはすべてのテキストボックスが含まれ、空の条件をチェックできます

于 2012-06-28T14:16:21.257 に答える
0

人々が指摘しているように、あなたのアプローチは間違っています - ページにコントロールを動的に追加しない限り、バリデーターを介して検証を実行する必要があります。

とにかくそれを行う方法のスニペットを次に示します。

private void SearchControls(Control controlSearch)
{
    foreach (Control control in controlSearch.Controls)
    {
        if (control != null)
        {
            if (control.Controls != null & control.Controls.Count > 0)
            {
                SearchControls(control, form);
            }

            TextBox textbox = control as TextBox;
            if (textbox != null && string.IsNullOrEmpty(textbox.Text))
            {

            }
        }
    }
}

ページ内で使用SearchControls(this)して、検索を開始します。

于 2012-06-28T14:18:31.117 に答える
0

ここに良い記事がありますが、以下は特定のプロパティによってコントロールを収集する修正版です

public List<Control> ListControlCollections(Page page, string propertyName)
{
    List<Control> controlList = new List<Control>();
    AddControls(page.Form.Controls, controlList, propertyName);
    return controlList;
}

private void AddControls(ControlCollection controlCollection, List<Control> controlList, string propertyName)
{
    foreach (Control c in controlCollection) {
        PropertyInfo propertyToFind = c.GetType().GetProperty(propertyName);

        if (propertyToFind != null) {
            controlList.Add(c);
        }

        if (c.HasControls()) {
            AddControls(c.Controls, controlList, propertyName);
        }
    }
}

使用するには:

List<Control> controlList = ListControlCollections("Text");
for (i=0; i < controlList.length; i++)
{
   if (controlList[i].Text == string.empty)
   {
      // Do your logic here
   }
   else  
   {
      // Do your logic here
   }
}
于 2012-06-28T13:57:41.727 に答える