Control is an ambiguous reference between System.Web.UI.Control and System.Windows.Forms.Control 文本框错误

Control is an ambigious reference between System.Web.UI.Control and System.Windows.Forms.Control error for Textboxes

我想清除 button click 上的 textboxes 值,但是当我写下面的代码时,出现错误

Control is an ambigious reference between System.Web.UI.Control and System.Windows.Forms.Control.

这是我的代码:-

public void CleartextBoxes(Control parent)
{
    foreach (Control x in parent.Controls)
    {
        if ((x.GetType() == typeof(TextBox)))
        {
            ((TextBox)(x)).Text = "";
        }
        if (x.HasControls())
        {
            CleartextBoxes(x);
        }
    }
}

我尝试了 here 中的 link,但它不符合我的要求。请提出任何帮助

显然您已经引用了库 System.Web.UISystem.Windows.Forms。这两个库都有一个名为 Control 的 class。所以你必须指定你想使用哪个库:

public void CleartextBoxes(System.Windows.Forms.Control parent)
{
    foreach (System.Windows.Forms.Control x in parent.Controls)
    {
        if ((x.GetType() == typeof(TextBox)))
        {
            ((TextBox)(x)).Text = "";
        }
        if (x.HasChildren)
        {
            CleartextBoxes(x);
        }
    }
}