我如何在asp.net中使用FindControl方法?

时间:2010-07-01 12:34:33

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

如何使用findControl,我如何根据FindControl方法获取id?我需要得到所有TextBox数据有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.... 
                }
            }

        }

alt text http://i49.tinypic.com/2mfb4ew.png

我只需要这个:

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

        }

    }

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

作为Pranay提供的答案的延伸,我认为你可能会对INamingContainer犯规。

使用嵌套控件名创建asp.net控件ID。有关webcontrols和INamingContainer接口的更多信息,请查看MSDN

答案 2 :(得分:0)

为什么要循环浏览每个控件,然后让页面找到您已有的控件?

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

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

答案 3 :(得分:0)

添加此扩展方法:

    /// <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 = "";
}