添加下拉列表 [英] Adding Dropdown list

查看:82
本文介绍了添加下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在根据用户输入的数字创建特定数量的下拉列表。我有文本框(用户输入数字如2)和创建下拉列表的按钮(在这种情况下是2个下拉列表)。如下所示

hi all,

i am working on creating specific number of drop down list depending on number inputs by user. i had textbox(where user input number like 2) and button that create drop down list (in this case 2 dropdown lists). as below

for (int i = 0; i <= Convert.ToInt16(DropDownList2.SelectedValue); i++)
{

    DropDownList dp = new System.Web.UI.WebControls.DropDownList();
    dp.ID = "ClRepList" + i;
    dp.AutoPostBack = true;
    dp.Height = 24;
    dp.Width = 102;
    dp.DataTextField = "PName";
    dp.DataValueField = "PName";
    dp.DataSource = return_data_table();
    dp.DataBind();
    ClientRepInfo.Controls.Add(dp);
}



现在我想从这些列表中读取值以保存数据库中的值。如下所示


now i want to read values from these lists to save values in database . as below

protected void Button1_Click(object sender, EventArgs e)
{
    string[] Path=new string[5];
    ContentPlaceHolder MainCont =Page.Master.FindControl("MainContent") as ContentPlaceHolder;
    for (int k = 0; k <= 4; k++)
    {
        Panel Dpanel = (Panel)MainCont.FindControl("ClientRepInfo") as Panel;
        DropDownList drp = (DropDownList)Dpanel.FindControl("ClRepList" + k);
            if (drp != null)
            //  if (MainCont.FindControl("ClRepList" + k) != null)
            {
                Path[k] = drp.SelectedValue;
            }        
    }



但现在可行了。当我在DropDown drp上设置断点时它读取空值!!



任何帮助请...


but this now work. when i put break point on DropDown drp it reads null value !!

any help please ...

推荐答案

foreach ( Control ctrl in panel) {

if(ctrl is DropDownList)
{
 Path[k] = ctrl .SelectedValue;
//let me know if it is giving null value
}
    //    
    }


您需要在每次回发时创建下拉列表,它们不会自动重新创建。
You need to create the dropdowns on every postback, they won't be recreated automatically.


这篇关于添加下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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