DropDownList Problem

Last post 12-22-2007 6:39 AM by Kawashgar. 9 replies.

Sort Posts:

  • DropDownList Problem

    03-19-2007, 4:44 PM
    • Loading...
    • amative
    • Joined on 11-04-2006, 4:17 PM
    • Posts 32

    hiiii

    i have a DropDownList1 that has a data source from a table, and the autopostback is enabled ... the problem is

    when DropDownList1 has only one item the event SelectedIndexChanged does not work ..

    but in case of 2 items and above it works fine  ........

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {Label1.Text = DropDownList1.SelectedItem.Text;}

  • Re: DropDownList Problem

    03-19-2007, 4:50 PM

    Off course! You have only one item and it is the one selected!! You should have more than one item to try to change selection and see how the page posts back!

    Regards

    Bilal Hadiar, MCP, MCTS, MCPD, MCT
    Microsoft MVP - Telerik MVP
  • Re: DropDownList Problem

    03-19-2007, 4:53 PM
    Answer
    • Loading...
    • docluv
    • Joined on 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 1,484
    • TrustedFriends-MVPs

    The way I get around this is to insert a new item at the top of the list to tell the user to make a selection from the list. You would do this after you have bound the dataset from the database to the ListControl.

    ddlMyList.Items.Insert(0, new ListItem("Select an Item"))

    This forces the user to make a selection from the list.

  • Re: DropDownList Problem

    03-19-2007, 5:03 PM
    • Loading...
    • amative
    • Joined on 11-04-2006, 4:17 PM
    • Posts 32

    hii i used this ... but no new items is added to the list

    protected void Page_Load(object sender, EventArgs e)

     { DropDownList1.Items.Insert(0, new ListItem("Select Item")); }

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    { Label1.Text = DropDownList1.SelectedItem.Text; }

  • Re: DropDownList Problem

    03-19-2007, 5:06 PM
    • Loading...
    • amative
    • Joined on 11-04-2006, 4:17 PM
    • Posts 32

    Note that the dropdown list datasource is Database table not a collection of items ....

     

  • Re: DropDownList Problem

    03-19-2007, 5:30 PM
    • Loading...
    • pixelsyndicate
    • Joined on 07-04-2003, 12:56 PM
    • W. MI transplant in N. TX
    • Posts 1,082

    Hey amative,

    You should simply get into the habit of creating DropDownLists with a default "empty" value.  Empty isn't really the term I would use, but it simply means that the selection of this value can be used in validation and it gives the user an idea as to what the dropdownlist will do for them. 

    Create your DropDownList with the following properties:

    AppendDataBountItems = true
    AutoPostBack = true
    DataSourceID = {your datasource name}
    DataTextField = {your text column}
    DataValueField = {your value column}

    Now, immediately add a new item to the dropdownlist (in the gui is fine)... The item should have the following values:

    Enabled = true
    Selected = true
    Text = "Please select a value"
    Value = 0

    Note that I do not leave the Value property blank; this will not stay blank during the process of building the website... somehow VS.NET 2005 gets the idea that the Value if blank should be changed to the Text value.  So I use ZERO (0) as my value since I know that my datasource will not contain a ZERO value.  Change it to "NOTHING" or something if you need to.

    What all this will do for you is allow your DataSource to append to the dropdownlist all of the values.  It also allows you to set validation in your form.  If I use ZERO for the value of my DataValueField I can use a rangevalidator to specifiy the validation fails if not > 0.    Now your selection events will work fine, and you can even check for that value=0 to hide or show other form elements. 

    protected void ddl_SelectedIndexChanged(object sender, EvenArgs e)
    {
    if (ddl.SelectedValue !- "0")
    {
          btnNew.Enabled = true;
          formviewUserControls.DefaultMode = FormViewMode.ReadOnly;

    }
    else
    {
          btnNew.Enabled = true;
    }
    }

     

    Wil

    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

    http://pixelsyndicate.com/ps/
  • Re: DropDownList Problem

    03-19-2007, 6:06 PM
    • Loading...
    • amative
    • Joined on 11-04-2006, 4:17 PM
    • Posts 32

    hiiiiii

    thanx for ur reply ...

    i tried your way ... but unfortunately its not working ... i did not succeed in adding new item to the DDL its only

    filled with the DataSource items ...

     

  • Re: DropDownList Problem

    03-19-2007, 6:12 PM
    Answer
    • Loading...
    • docluv
    • Joined on 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 1,484
    • TrustedFriends-MVPs

    It is not working because it looks like you are releading your dropdown list each time the page is loaded. you need to check the IsPostback = false then bind the list to the control, then the empty item.

    Page_Load

       If IsPostback then

         BindDDLData()

       end if

    end sub

     

    sub      BindDDLData()

    ddl.datasource = XYZ

    ddl.BindData

    ddl.items.insert(0, new ListItem) 

     

    end sub

  • Re: DropDownList Problem

    03-19-2007, 9:26 PM
    Answer

    Hello friend,

    try the following

    public partial class BindDataList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindDropDownList();

        }
        void BindDropDownList()
        {
            string connect = "Data Source=localhost\\sqlexpress;Initial Catalog=pubs;Integrated Security=True;";
            System.Data.SqlClient.SqlConnection sqlconnect = new System.Data.SqlClient.SqlConnection(connect);
            string command = "select job_desc from jobs";
            System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
            sqlcommand.CommandText = command;
            sqlcommand.Connection = sqlconnect;

            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
            da.SelectCommand = sqlcommand;
            DataSet ds = new DataSet();

            sqlconnect.Open();
            sqlcommand.ExecuteNonQuery();
            da.Fill(ds);
            DropDownList1.DataTextField = "job_desc";
            DropDownList1.DataValueField = "job_desc";
            DropDownList1.DataSource = ds;
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("-SELECT-"));
            sqlconnect.Close();
        }
      }

    hope it helps,

    Jessica

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: DropDownList Problem

    12-22-2007, 6:39 AM
    • Loading...
    • Kawashgar
    • Joined on 12-21-2007, 6:58 AM
    • Singapore
    • Posts 5

    amative:

     Hi friend r u still having this problem.

    friend if u still facing problem-

    right click drop down list and select edit items --> Enter any text and click OK

     
    then change the  

    AppendDataBoundItem  Property to True

     Hope this helps you. Yes


    hiiii

    i have a DropDownList1 that has a data source from a table, and the autopostback is enabled ... the problem is

    when DropDownList1 has only one item the event SelectedIndexChanged does not work ..

    but in case of 2 items and above it works fine  ........

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {Label1.Text = DropDownList1.SelectedItem.Text;}

     
    Best Regards,

    Kavashgar M
    " Proud to be Tamilan "
    --------------------------------------------------
    From Country(Tamil Eelam) "Nothing Impossible - The Word itself says Im - possible" - So Keep trying
Page 1 of 1 (10 items)
Microsoft Communities
Page view counter