在ASP.net中创建一个动态DropdownList [英] Create a Dynamic DropdownList in ASP.net

查看:94
本文介绍了在ASP.net中创建一个动态DropdownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按钮中创建一个动态下拉列表单击事件和具有

相同值的下拉菜单

我试过但没有用

I Want to Create a Dynamic Dropdown list in the button Click Event and the drop downs having the
same values
I Have tried this but no use

protected void btnSet_Click(object sender, EventArgs e)
    {
        foreach (TableRow tr in tbl.Controls )
        {
            foreach (TableCell tc in tr.Controls)
            {
                if (tc.Controls[0] is DropDownList)
                {
                           
                }
            }
           
        }
               
    }





下拉列表值相同。但只有点击按钮才能创建下拉菜单

活动。



请建议我获得解决方案



先谢谢



the Dropdown list values are same. But the drop downs will create only in the button click
Event.

Please Suggest me to get the Solution

Thanks in Advance

推荐答案

假设您有字符串列表并需要创建动态DropDownList



assume you have list of strings and need to create dynamic DropDownList

DropDownList ddl= new DropDownList();
foreach (string item in stringList){
    ddl.Items.Add(new ListItem(item));
}
form1.Controls.Add(ddl);





如果你需要绑定数据表



if you need to bind datatable

DropDownList ddl= new DropDownList();
ddl.DataSource = ddl
ddl.DataValueField = "value field name"
ddl.DataTextField = "textfield name"
form1.Controls.Add(ddl);
ddl.DataBind()


这篇关于在ASP.net中创建一个动态DropdownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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