【问题标题】:How to use for loop to scan all controls in ASP .NET page?如何使用 for 循环扫描 ASP .NET 页面中的所有控件?
【发布时间】:2012-06-30 01:28:57
【问题描述】:
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,我有一个带有 texboxes 的插入页面,我需要检查页面中的 texboxes 是否为空,如果是,我需要显示一个带有空文本框的消息框

【问题讨论】:

  • 尝试使用Validation 控件(客户端和服务器端)或JavaScript (jQuery) 来验证用户输入。
  • 您没有显示任何实际的消息框是吗?这会在你部署时引发各种问题。

标签: c# asp.net servercontrols


【解决方案1】:

这里是a good article on it,但下面是修改后的版本,它按给定属性收集控件

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

【讨论】:

  • @AVD 我已经尝试过使用 javascript,但问题是我有一个母版页和内容页,所以 id 不同,当我使用 clientid proerty 时 javascript 代码不起作用
  • 我需要循环页面中的所有控件,如果它的类型是文本框,我需要检查它是否为空,如果它为空,则应该出现一个带有空文本框的消息框和回滚以将文本放入空的文本中
  • 感谢您的代码,但它将如何帮助我解决这个问题?
  • 我更新了答案。上面的代码将收集页面上的所有控件。一旦你拥有了所有的控件,你就可以遍历它们并检查它们的所有值。它对您的情况非常有用。
  • @OmniaElshazly - 您可以在 JavaScript/jQuery 中设置 Control.ClientIdMode=Static 和 Control.ClientID 来引用服务器控件的 ID。
【解决方案2】:

我猜你可以试试这个递归函数来获取页面上的所有文本框。

    /// <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 将包含所有文本框,然后您可以检查它们是否为空

【讨论】:

    【解决方案3】:

    正如人们所指出的那样,您的方法是错误的 - 除非您向页面动态添加控件,否则您应该通过验证器执行验证。

    这里有一个关于如何做的sn-p:

    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) 开始搜索。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多