Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

Copying Dropdownlists

Last post 09-06-2007, 4:57 AM by Stu. 4 replies.
Sort Posts: Previous Next
  •  09-05-2007, 12:05 PM 34502

    Copying Dropdownlists

    Hi all,

    I'm building an appointment booking system. I'm allowing the user to create Master Days, with Master Booking Slots, from which they can create date-specific Days with Slots.

    Each Master Day can have a different number of slots, so I build the set of textboxes (for start time) and dropdownlists (for services offered at that time) dynamically.

    My problem is copying the set of services into each of the dynamic dropdownlists.

    I've created a hidden ddl from the database, populated depending upon certain criteria. I then use the following code :

    DropDownList ddlCat = (DropDownList)this.pnlMDaySlots.FindControl("ddlCat" + Convert.ToString(i + 1));

    DropDownList ddlCat = (DropDownList)this.pnlMDaySlots.FindControl("ddlCat" + Convert.ToString(i + 1));

    ddlCat.DataSource = ddlCat_Hidden.Items;

    ddlCat.DataBind();

     

    Which copies all the items perfectly, BUT sets the Value to the same as the Item (i.e. the text of the Service), instead of the original database unique ID value which is stored in the hidden ddl.

    So, my question is - how do I copy the items from the hidden ddl into each of the dynamic ddls, and preserve the Value and Item contents ???

     

    Any help gratefully received.

    Stu 

     

    , the number of each is dependant upon a pre-built list

    Filed under:
  •  09-05-2007, 1:26 PM 34506 in reply to 34502

    Re: Copying Dropdownlists

    Hi,

    if you put

    ddlCat.DataTextField = "Text";
    ddlCat.DataValueField = "Value";

    Does that help?

     


    Thanks,

    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
  •  09-06-2007, 3:54 AM 34538 in reply to 34506

    Re: Copying Dropdownlists

    Hi Joteke,

    Yeah, I've tried that, and I get a run-time error :

              DropDownList ddlCat = (DropDownList)this.pnlMDaySlots.FindControl("ddlCat" + Convert.ToString(i + 1));

              ddlCat.DataValueField = "cat_id";

             ddlCat.DataTextField = "cat_name";

             ddlCat.DataSource = ddlCat_Hidden.Items;

            ddlCat.DataBind();

     

                                        DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'cat_name'.

     Similarly, if I try to make the new ddl DataValueField & DataTextField equal to the hidden ddl fields, I get the same problem.

    I might just use a FOR loop to populate each occurrence, but I thought there ought to be an easy way to do this.

    Thanks.

     Stu

  •  09-06-2007, 4:08 AM 34540 in reply to 34538

    Re: Copying Dropdownlists

    Yes, put it literally as I posted. ListItem does have Value and Text proeprties

    ddlCat.DataTextField = "Text";
    ddlCat.DataValueField = "Value";


    Thanks,

    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
  •  09-06-2007, 4:57 AM 34542 in reply to 34540

    Re: Copying Dropdownlists

    Wow thanks !!!!!!!!!!!!!

    I've seen code using the 'Text' and 'Value' strings, but didn't realise that's what I needed to put.

    Many thanks !! 

     

View as RSS news feed in XML