DropDownList 怎么在后台用代码生产下拉列表项和怎么用代码给他赋值呢?

解决方案 »

  1.   

    DropDownList1.Item.Insert(0,new ListItem("请选择",0)); 
      

  2.   

    LiseItem item =new ListItem(text,value);
    this.dropdownlist1.item.add(item);
      

  3.   


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Tim 演示</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div> 
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>    </div>
        </form>
    </body>
    </html>  protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        DropDownList1.Items.Add(new ListItem(i.ToString(),(i*10).ToString()));
                    }
                }
            }
      

  4.   

    DropDownList1.SelectedValue="0";
    DropDownList1.Text="请选择";
      

  5.   

    DropDownList1.Item.Insert(0,new ListItem("显示值",索引));
     
      

  6.   

    在前台页面放一个label 如:<asp:Label ID="lblSelect" runat="server" Text=""></asp:Label>然后在后台给lblSelect.Text属性赋值,输出一个DropDownList 可以了,DropDownList 里的内容根据你实际情况赋值,如张三,李四,王五,赵六
    lblSelect.Text="<select id=\"myDropDownList\" style=\"width:145px;\">
    <option selected=\"selected\" value=\"0\">张三</option>
    <option value=\"1\">李四</option>
    <option value=\"2\">王五</option>
    <option value=\"3\">赵六</option> 
    </select>";
      

  7.   

    dropdownlist.items.insert(0,new listitem("-请选择-","")) 
    dropdownlist.SeletedValue=""
      

  8.   

    for (int i = 0; i < 5;i++)
            {
                ListItem list = new ListItem();
                list.Text = "AAA"+i;
                this.DropDownList1.Items.Add(list);
            }        this.DropDownList1.DataSource = dt;
            this.DropDownList1.DataTextField = "title";
            this.DropDownList1.DataValueField = "id";  
            this.DropDownList1.DataBind();