Dropdownlist is not accepting new items when added dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagdeep gupta
    New Member
    • Aug 2010
    • 98

    #1

    Dropdownlist is not accepting new items when added dynamically

    On adding items in it ,shows an error: "Dropdownli st does not allow multiple selection."
    But when i tries to print the selected item it shows null exception.
  • C Sharper
    New Member
    • Oct 2010
    • 9

    #2
    Could you include some of your code as an example?

    Comment

    • jagdeep gupta
      New Member
      • Aug 2010
      • 98

      #3
      Code:
      using System;
      using System.Collections;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Xml.Linq;
      
      public partial class Expt : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
             
              
          }
          protected void Bttnadd_Click(object sender, EventArgs e)
          {
              
             DropDownList11.Items.Add(ListBox1.SelectedItem);
              ListBox1.Items.Remove(ListBox1.SelectedItem);
              
              
          }

      Comment

      • C Sharper
        New Member
        • Oct 2010
        • 9

        #4
        could you provide the page too, so I can see what the code behind is working with?

        Comment

        • jagdeep gupta
          New Member
          • Aug 2010
          • 98

          #5
          Pages with codes for this

          Please check the attached files
          Attached Files

          Comment

          • C Sharper
            New Member
            • Oct 2010
            • 9

            #6
            By default the first item of a dropdown is selected. try setting the selectedindex of the dropdown to -1 before adding the new item...

            protected void bttnaddck_Click (object sender, EventArgs e)
            {
            DropDownList1.S electedIndex = -1;
            DropDownList1.I tems.Add(ListBo x1.SelectedItem );
            ListBox1.Items. Remove(ListBox1 .SelectedItem);
            }

            Comment

            • jagdeep gupta
              New Member
              • Aug 2010
              • 98

              #7
              U try to print the selected item it will show Null Exception error why so?

              Comment

              Working...