加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 访问以编程方式创建的控件

发布时间:2020-12-16 01:59:25 所属栏目:百科 来源:网络整理
导读:我在占位符中创建了一系列面板,每个面板内部有5个文本框. 按下按钮,我想从每个文本框中获取.Text,以及Panel的ID.我计划使用JaggedArray来跟踪所有Panel及其内部TextBox. string[][] jaggesaddress = new string[stop][]; //stop is an int and equal to amou
我在占位符中创建了一系列面板,每个面板内部有5个文本框.

按下按钮,我想从每个文本框中获取.Text,以及Panel的ID.我计划使用JaggedArray来跟踪所有Panel及其内部TextBox.

string[][] jaggesaddress = new string[stop][]; //stop is an int and equal to amount of panels on screen.
    String[] numbers = new String[stop];

    int a = 0;

    //Control ctrl;

    foreach (Control ctrl in BookingPlaceholder.Controls) //BookingPlaceholder is placehodler ID
    {
        if (ctrl is Panel)
        {
            numbers[a] = ctrl.ID;
            a++;
            //ctrl.ID = numbers[a];

            foreach(TextBox tb in ctrl.ID)  // error here
            {

            }
        }
    }

然而,第二个foreach循环有一个错误,我不是100%肯定如何克服这个.

“Cannot convert type ‘char’ to ‘System.Web.UI.WebControls.TextBox'”

我知道数字[a]确实找到了第一个面板,但它是一个没有ID扩展名的字符串.

任何建议表示赞赏.

>乔希

解决方法

如果对于PlaceHolder中可用的更多通用控件那么
你需要找到文本框控件.因此也可以安全地检查控制类型.

foreach (Control ctr in ctrl.Controls)
        {
            if (ctr is TextBox)
            {
                //Do your things
                // ((TextBox)ctr).Text 
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读