【问题标题】:How can i use FindControl method in asp.net?如何在 asp.net 中使用 FindControl 方法?
【发布时间】:2011-03-10 14:58:47
【问题描述】:

如何使用 findControl 以及如何根据 FindControl 方法获取 id?我需要获取所有文本框数据,有 40 个文本框。还有 TextBoxid 数据...

我也很想学习 linq 方法;)
  protected void Button1_Click(object sender, EventArgs e)
        {
          //  SetRecursiveTextBoxAndLabels(PlaceHolder1);
            CreateForm creater = new CreateForm();
            creater.Holder = PlaceHolder1;
            creater.SetAccessForm();

            if (PlaceHolder1.Controls.Count > 0)
            {
                foreach (Control item in PlaceHolder1.Controls)
                {
                    item.FindControl(item.ID) is TextBox-------> How can i control is textBox?  Because there are labels grid.... 
                }
            }

        }

我只需要这个:

    ENG_ACCESS engAccess = new ENG_ACCESS();
            Type engTyp = engAccess.GetType();
            PropertyInfo[] properties = engTyp.GetProperties();

            TextBox txtbox = new TextBox();

            foreach (PropertyInfo strColumn in properties)
              {
                  txtbox = (TextBox)Page.FindControl("txt" + strColumn.Name);
                ListBox1.Items.Add(txtbox.Text);
            }

我的完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

namespace RecursiveAddTextBox
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var dataCtx = new DataClasses1DataContext())
            {

                if (!IsPostBack)
                {
                    SetRecursiveTextBoxAndLabels();
                }



            }

        }

        void SetRecursiveTextBoxAndLabels()
        {
            TextBox txtBox;
            Label lbl;

                ENG_ACCESS eng = new ENG_ACCESS();
                Type typ = eng.GetType();
                PropertyInfo[] properties = typ.GetProperties();
                PlaceHolder1.Controls.Add(new LiteralControl("<table>"));
                for( int i =0;  i<properties.Length; i++)
                {
                    lbl = new Label();
                    lbl.ID = "lbl" + properties[i].Name;
                    lbl.Text = properties[i].Name;
                    PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                    PlaceHolder1.Controls.Add(lbl);
                    PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                    txtBox = new TextBox();
                    txtBox.ID ="txt"+properties[i].Name;
                    PlaceHolder1.Controls.Add(txtBox);
                    PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                }
                PlaceHolder1.Controls.Add(new LiteralControl("</table>"));

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           SetRecursiveTextBoxAndLabels();



        }
        protected void Button2_Click(object sender, EventArgs e)
        {

            if (PlaceHolder1.Controls.Count > 0)
            {
                foreach (Control item in PlaceHolder1.Controls)
                {
                    if (item is TextBox)
                    {
                        TextBox t1 = (TextBox)PlaceHolder1.FindControl(item.ID);
                    }
                }
            }

        }

    }

【问题讨论】:

  • 如果您在这里要做的只是通过页面上的反射显示给定对象的属性并对其进行编辑,为什么不使用 FormView 控件并将其数据绑定到您的数据?

标签: c# .net asp.net visual-studio linq


【解决方案1】:

作为 Pranay 提供的答案的扩展,我认为您可能与 INamingContainer 发生了冲突。

asp.net 控件 ID 是使用嵌套控件名称创建的。查看 MSDN 以获取有关 web 控件和 INamingContainer 接口的更多信息

【讨论】:

    【解决方案2】:
    protected void Button1_Click(object sender, EventArgs e) 
            { 
              //  SetRecursiveTextBoxAndLabels(PlaceHolder1); 
                CreateForm creater = new CreateForm(); 
                creater.Holder = PlaceHolder1; 
                creater.SetAccessForm(); 
    
                if (PlaceHolder1.Controls.Count > 0) 
                { 
                    foreach (Control item in PlaceHolder1.Controls) 
                    { 
                         if (item is TextBox)
                             TextBox t1=(TextBox)PlaceHolder1.FindControl(item.ID);
                    } 
                } 
            }
    

    【讨论】:

    • 如果这些是您正在创建的动态控件,那么您可能在创建和添加控件之前调用了上述方法。该方法何时被调用?
    【解决方案3】:

    你为什么要遍历每个控件,然后要求页面找到你已经拥有的控件?

    换句话说,如果 (item is TextBox) 那么 item 就是你要找的 TextBox!

    请记住,FindControl 不是递归的,因此您必须在实际包含您要查找的控件的命名容器实例上调用 FindControl。

    【讨论】:

      【解决方案4】:

      添加这个扩展方法:

          /// <summary>
          /// Finds all controls with the given type anywhere under the root
          /// </summary>
          public static IList<Control> FindControlsRecursive<FindType>( this ControlCollection root )
          {
              Type toFind = typeof( FindType );
              List<Control> controls = new List<Control>();
              if ( root != null && root.Count > 0 )
              {
                  foreach ( Control control in root )
                  {
                      if ( control != null && ( toFind.IsAssignableFrom( control.GetType() ) ) )
                      {
                          controls.Add( control );
                      }
                      if ( control != null )
                      {
                          controls.AddRange( control.Controls.FindControlsRecursive<FindType>() );
                      }
                  }
              }
              return controls;
          }
      

      然后调用它:

      var textBoxes = Page.Controls.FindControlsRecursive<TextBox>();
      foreach(var tb in textBoxes)
      {
          tb.Text = "";
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多