动态添加控件,并在单击按钮时读取其中的值. [英] Dynamically adding controls and read there values on button click.

查看:61
本文介绍了动态添加控件,并在单击按钮时读取其中的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我想动态添加一些控件,即我将为用户提供一个带有项目"的下拉列表,例如(TextBox,DropdownList,Button,DateTime等),因此当用户选择任何项目时,它将添加到提供的空间中页面.

例如:当用户从下拉列表中选择文本框"时,将添加一个文本框,对于按钮",将添加一个Button,当用户单击该动态添加的按钮时,我要将输入的文本保存在文本框中. br/>

请帮助我,,,

在此先感谢...

Dear friends,

I want to add some controls dynamically i.e. I will provide a dropdownlist to the users with "items" like(TextBox,DropdownList,Button,DateTime,etc),so when user will select any of the item it is added to the space provided in the page.

for example: when user will select "Textbox" from the dropdown,a text box is added,for "Button" a Button will add and when user will click on that dynamically added button ,i want to save the entered text in textbox.


Please help me ,,,

Thanks in advance...

推荐答案

很简单.假设您使用面板来保存控件(它只是使控件保持一致并使代码更简单)
It''s pretty easy. Assuming that you use a Panel to hold the controls (it just keeps them togther and makes the code simpler)
Button b = new Button();
    b.Click += new EventHandler(MyDynamicButton_Click);
    MyPanel.Controls.Add(b);
    }

void MyDynamicButton_Click(object sender, EventArgs e)
    {
    foreach (Control c in MyPanel.Controls)
        {
        TextBox tb = c as TextBox;
        if (tb != null)
            {
            Console.WriteLine(tb.Text);
            }
        }
    }

您可以执行类似的操作来添加文本框.您可能需要设置按钮位置"和文本"属性.

You do a similar thing to add your text box. You will probably need to set the Button Location and Text properties, among others.


这篇关于动态添加控件,并在单击按钮时读取其中的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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