如何在asp.net中使用FindControl方法? [英] How can i use FindControl method in asp.net?

查看:108
本文介绍了如何在asp.net中使用FindControl方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用findControl,如何根据FindControl方法获取ID?我需要获取所有TextBox数据,其中有40个文本框.还有TextBoxid数据...

How can i use findControl and how can i get id's according to FindControl method? i need to get all TextBox data there are 40 textbox. And TextBoxid data...

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

        }

    }

推荐答案

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

这篇关于如何在asp.net中使用FindControl方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆