databinding a Dropdownlist to another dropdownlist

Discussion in 'ASP .Net' started by tshad, Oct 14, 2005.

  1. tshad

    tshad Guest

    Can you databind a dropdownlist to another dropdownlist?

    I have 2 identical list. I am getting my data from a DataReader, so as soon
    as I have bound the first DDL, I can't do it again to the next as the
    dataReader can only be read once.

    I tried:

    ExperienceLevel.DataSource=dbReader
    ExperienceLevel.DataTextField= "Description"
    ExperienceLevel.DataValueField="ExperienceLevelID"
    ExperienceLevel.databind()
    ExperienceLevel.Items.Insert(0, "Select Experience Level")
    ExperienceLevel.Items(0).value = 0

    ActualExperienceLevel.DataSource=ExperienceLevel
    ActualExperienceLevel.DataTextField= DataTextField
    ActualExperienceLevel.DataValueField=DataValueField
    ActualExperienceLevel.databind()
    ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    ActualExperienceLevel.Items(0).value = 0

    But that doesn't seem to work.

    Thanks,

    Tom
    tshad, Oct 14, 2005
    #1
    1. Advertising

  2. tshad

    Guest

    Fill an ArrayList with DictionaryEntries, then bind to that.

    Example
    ArrayList arlItems = new ArrayList ();
    while (dReader.Read ())
    arlItem.Add (new DictionaryEntry (dReader["Description"].ToString (),
    dReader["ExperienceLevelID].ToString());

    then create an array and set the datasources

    ActualExperienceLevel.DataSource = arrDEntries;
    ActualExperienceLevel.DataTextField = "value";
    ActualExperienceLevel.DataValueField = "key";

    Hope this helps,
    Brendan
    , Oct 14, 2005
    #2
    1. Advertising

  3. To bind to a dropdownlist the datasource must implement IListSource or
    IEnumerable. DropDownList.Items is enumerable.

    So this will work:
    ExperienceLevel.DataSource=dbReader
    ExperienceLevel.DataTextField= "Description"
    ExperienceLevel.DataValueField="ExperienceLevelID"
    ExperienceLevel.DataBind()
    ExperienceLevel.Items.Insert(0, "Select Experience Level")
    ExperienceLevel.Items(0).value = 0

    ActualExperienceLevel.DataSource=ExperienceLevel.Items
    ActualExperienceLevel.DataBind()

    Don't add your default item because it is already in the list.

    Tim

    "tshad" wrote:

    > Can you databind a dropdownlist to another dropdownlist?
    >
    > I have 2 identical list. I am getting my data from a DataReader, so as soon
    > as I have bound the first DDL, I can't do it again to the next as the
    > dataReader can only be read once.
    >
    > I tried:
    >
    > ExperienceLevel.DataSource=dbReader
    > ExperienceLevel.DataTextField= "Description"
    > ExperienceLevel.DataValueField="ExperienceLevelID"
    > ExperienceLevel.databind()
    > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    > ExperienceLevel.Items(0).value = 0
    >
    > ActualExperienceLevel.DataSource=ExperienceLevel
    > ActualExperienceLevel.DataTextField= DataTextField
    > ActualExperienceLevel.DataValueField=DataValueField
    > ActualExperienceLevel.databind()
    > ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    > ActualExperienceLevel.Items(0).value = 0
    >
    > But that doesn't seem to work.
    >
    > Thanks,
    >
    > Tom
    >
    >
    >
    =?Utf-8?B?dGlta2xpbmc=?=, Oct 15, 2005
    #3
  4. tshad

    tshad Guest

    "timkling" <> wrote in message
    news:D...
    > To bind to a dropdownlist the datasource must implement IListSource or
    > IEnumerable. DropDownList.Items is enumerable.
    >
    > So this will work:
    > ExperienceLevel.DataSource=dbReader
    > ExperienceLevel.DataTextField= "Description"
    > ExperienceLevel.DataValueField="ExperienceLevelID"
    > ExperienceLevel.DataBind()
    > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    > ExperienceLevel.Items(0).value = 0
    >
    > ActualExperienceLevel.DataSource=ExperienceLevel.Items
    > ActualExperienceLevel.DataBind()
    >


    Worked great.

    > Don't add your default item because it is already in the list.


    Which default item - the insert?

    ExperienceLevel.Items.Insert(0, "Select Experience Level")


    Thanks,

    Tom
    >
    > Tim
    >
    > "tshad" wrote:
    >
    >> Can you databind a dropdownlist to another dropdownlist?
    >>
    >> I have 2 identical list. I am getting my data from a DataReader, so as
    >> soon
    >> as I have bound the first DDL, I can't do it again to the next as the
    >> dataReader can only be read once.
    >>
    >> I tried:
    >>
    >> ExperienceLevel.DataSource=dbReader
    >> ExperienceLevel.DataTextField= "Description"
    >> ExperienceLevel.DataValueField="ExperienceLevelID"
    >> ExperienceLevel.databind()
    >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> ExperienceLevel.Items(0).value = 0
    >>
    >> ActualExperienceLevel.DataSource=ExperienceLevel
    >> ActualExperienceLevel.DataTextField= DataTextField
    >> ActualExperienceLevel.DataValueField=DataValueField
    >> ActualExperienceLevel.databind()
    >> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> ActualExperienceLevel.Items(0).value = 0
    >>
    >> But that doesn't seem to work.
    >>
    >> Thanks,
    >>
    >> Tom
    >>
    >>
    >>
    tshad, Oct 15, 2005
    #4
  5. tshad

    Bruce Barker Guest

    a datareader can not be used twice, because it forward only cursor. the
    second databind will find itself at the end.

    -- bruce (sqlwork.com)


    "tshad" <> wrote in message
    news:...
    > Can you databind a dropdownlist to another dropdownlist?
    >
    > I have 2 identical list. I am getting my data from a DataReader, so as
    > soon as I have bound the first DDL, I can't do it again to the next as the
    > dataReader can only be read once.
    >
    > I tried:
    >
    > ExperienceLevel.DataSource=dbReader
    > ExperienceLevel.DataTextField= "Description"
    > ExperienceLevel.DataValueField="ExperienceLevelID"
    > ExperienceLevel.databind()
    > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    > ExperienceLevel.Items(0).value = 0
    >
    > ActualExperienceLevel.DataSource=ExperienceLevel
    > ActualExperienceLevel.DataTextField= DataTextField
    > ActualExperienceLevel.DataValueField=DataValueField
    > ActualExperienceLevel.databind()
    > ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    > ActualExperienceLevel.Items(0).value = 0
    >
    > But that doesn't seem to work.
    >
    > Thanks,
    >
    > Tom
    >
    Bruce Barker, Oct 15, 2005
    #5
  6. > Which default item - the insert?
    >
    > ExperienceLevel.Items.Insert(0, "Select Experience Level")

    Yes. Do it to the first list, but after that it will be in the second list
    automatically.
    Tim

    "tshad" wrote:

    > "timkling" <> wrote in message
    > news:D...
    > > To bind to a dropdownlist the datasource must implement IListSource or
    > > IEnumerable. DropDownList.Items is enumerable.
    > >
    > > So this will work:
    > > ExperienceLevel.DataSource=dbReader
    > > ExperienceLevel.DataTextField= "Description"
    > > ExperienceLevel.DataValueField="ExperienceLevelID"
    > > ExperienceLevel.DataBind()
    > > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    > > ExperienceLevel.Items(0).value = 0
    > >
    > > ActualExperienceLevel.DataSource=ExperienceLevel.Items
    > > ActualExperienceLevel.DataBind()
    > >

    >
    > Worked great.
    >
    > > Don't add your default item because it is already in the list.

    >
    > Which default item - the insert?
    >
    > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >
    >
    > Thanks,
    >
    > Tom
    > >
    > > Tim
    > >
    > > "tshad" wrote:
    > >
    > >> Can you databind a dropdownlist to another dropdownlist?
    > >>
    > >> I have 2 identical list. I am getting my data from a DataReader, so as
    > >> soon
    > >> as I have bound the first DDL, I can't do it again to the next as the
    > >> dataReader can only be read once.
    > >>
    > >> I tried:
    > >>
    > >> ExperienceLevel.DataSource=dbReader
    > >> ExperienceLevel.DataTextField= "Description"
    > >> ExperienceLevel.DataValueField="ExperienceLevelID"
    > >> ExperienceLevel.databind()
    > >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    > >> ExperienceLevel.Items(0).value = 0
    > >>
    > >> ActualExperienceLevel.DataSource=ExperienceLevel
    > >> ActualExperienceLevel.DataTextField= DataTextField
    > >> ActualExperienceLevel.DataValueField=DataValueField
    > >> ActualExperienceLevel.databind()
    > >> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    > >> ActualExperienceLevel.Items(0).value = 0
    > >>
    > >> But that doesn't seem to work.
    > >>
    > >> Thanks,
    > >>
    > >> Tom
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?dGlta2xpbmc=?=, Oct 17, 2005
    #6
  7. tshad

    tshad Guest

    "timkling" <> wrote in message
    news:...
    >> Which default item - the insert?
    >>
    >> ExperienceLevel.Items.Insert(0, "Select Experience Level")

    > Yes. Do it to the first list, but after that it will be in the second
    > list
    > automatically.


    Worked as advertised.

    Thanks,

    Tom

    > Tim
    >
    > "tshad" wrote:
    >
    >> "timkling" <> wrote in message
    >> news:D...
    >> > To bind to a dropdownlist the datasource must implement IListSource or
    >> > IEnumerable. DropDownList.Items is enumerable.
    >> >
    >> > So this will work:
    >> > ExperienceLevel.DataSource=dbReader
    >> > ExperienceLevel.DataTextField= "Description"
    >> > ExperienceLevel.DataValueField="ExperienceLevelID"
    >> > ExperienceLevel.DataBind()
    >> > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> > ExperienceLevel.Items(0).value = 0
    >> >
    >> > ActualExperienceLevel.DataSource=ExperienceLevel.Items
    >> > ActualExperienceLevel.DataBind()
    >> >

    >>
    >> Worked great.
    >>
    >> > Don't add your default item because it is already in the list.

    >>
    >> Which default item - the insert?
    >>
    >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >>
    >>
    >> Thanks,
    >>
    >> Tom
    >> >
    >> > Tim
    >> >
    >> > "tshad" wrote:
    >> >
    >> >> Can you databind a dropdownlist to another dropdownlist?
    >> >>
    >> >> I have 2 identical list. I am getting my data from a DataReader, so as
    >> >> soon
    >> >> as I have bound the first DDL, I can't do it again to the next as the
    >> >> dataReader can only be read once.
    >> >>
    >> >> I tried:
    >> >>
    >> >> ExperienceLevel.DataSource=dbReader
    >> >> ExperienceLevel.DataTextField= "Description"
    >> >> ExperienceLevel.DataValueField="ExperienceLevelID"
    >> >> ExperienceLevel.databind()
    >> >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> >> ExperienceLevel.Items(0).value = 0
    >> >>
    >> >> ActualExperienceLevel.DataSource=ExperienceLevel
    >> >> ActualExperienceLevel.DataTextField= DataTextField
    >> >> ActualExperienceLevel.DataValueField=DataValueField
    >> >> ActualExperienceLevel.databind()
    >> >> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> >> ActualExperienceLevel.Items(0).value = 0
    >> >>
    >> >> But that doesn't seem to work.
    >> >>
    >> >> Thanks,
    >> >>
    >> >> Tom
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
    tshad, Oct 17, 2005
    #7
  8. tshad

    tshad Guest

    "timkling" <> wrote in message
    news:...
    >> Which default item - the insert?
    >>
    >> ExperienceLevel.Items.Insert(0, "Select Experience Level")

    > Yes. Do it to the first list, but after that it will be in the second
    > list
    > automatically.
    > Tim


    Just realized that it wasn't doing it correctly.

    I have the following:

    CopyResumeTo1.DataSource = dbReader
    CopyResumeTo1.DataValueField = "UserID"
    CopyResumeTo1.DataTextField = "FullName"
    CopyResumeTo1.DataBind()
    CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0"))

    CopyResumeTo2.DataSource=CopyResumeTo1.Items
    CopyResumeTo2.DataBind()

    CopyResumeTo3.DataSource=CopyResumeTo1.Items
    CopyResumeTo3.DataBind()

    The first one (CopyResumeTo1) is working correctly. It has the
    DataValueField (UserID) as an integer and the DataTextFiled (FullName) as
    the name and if I look at the ViewSource, it is correct.

    But when I look at the other 2 dropdowns, it is putting the FullName in both
    the DataValueField and the DataTextField.

    Also, I thought my insert would put a 0 in the Value field, but it put a
    blank there. Is my format incorrect?

    CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0")

    Thanks,

    Tom
    >
    > "tshad" wrote:
    >
    >> "timkling" <> wrote in message
    >> news:D...
    >> > To bind to a dropdownlist the datasource must implement IListSource or
    >> > IEnumerable. DropDownList.Items is enumerable.
    >> >
    >> > So this will work:
    >> > ExperienceLevel.DataSource=dbReader
    >> > ExperienceLevel.DataTextField= "Description"
    >> > ExperienceLevel.DataValueField="ExperienceLevelID"
    >> > ExperienceLevel.DataBind()
    >> > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> > ExperienceLevel.Items(0).value = 0
    >> >
    >> > ActualExperienceLevel.DataSource=ExperienceLevel.Items
    >> > ActualExperienceLevel.DataBind()
    >> >

    >>
    >> Worked great.
    >>
    >> > Don't add your default item because it is already in the list.

    >>
    >> Which default item - the insert?
    >>
    >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >>
    >>
    >> Thanks,
    >>
    >> Tom
    >> >
    >> > Tim
    >> >
    >> > "tshad" wrote:
    >> >
    >> >> Can you databind a dropdownlist to another dropdownlist?
    >> >>
    >> >> I have 2 identical list. I am getting my data from a DataReader, so as
    >> >> soon
    >> >> as I have bound the first DDL, I can't do it again to the next as the
    >> >> dataReader can only be read once.
    >> >>
    >> >> I tried:
    >> >>
    >> >> ExperienceLevel.DataSource=dbReader
    >> >> ExperienceLevel.DataTextField= "Description"
    >> >> ExperienceLevel.DataValueField="ExperienceLevelID"
    >> >> ExperienceLevel.databind()
    >> >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> >> ExperienceLevel.Items(0).value = 0
    >> >>
    >> >> ActualExperienceLevel.DataSource=ExperienceLevel
    >> >> ActualExperienceLevel.DataTextField= DataTextField
    >> >> ActualExperienceLevel.DataValueField=DataValueField
    >> >> ActualExperienceLevel.databind()
    >> >> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    >> >> ActualExperienceLevel.Items(0).value = 0
    >> >>
    >> >> But that doesn't seem to work.
    >> >>
    >> >> Thanks,
    >> >>
    >> >> Tom
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
    tshad, Oct 19, 2005
    #8
  9. tshad

    tshad Guest

    "tshad" <> wrote in message
    news:u$...
    >
    > "timkling" <> wrote in message
    > news:...
    >>> Which default item - the insert?
    >>>
    >>> ExperienceLevel.Items.Insert(0, "Select Experience Level")

    >> Yes. Do it to the first list, but after that it will be in the second
    >> list
    >> automatically.
    >> Tim

    >
    > Just realized that it wasn't doing it correctly.
    >
    > I have the following:
    >
    > CopyResumeTo1.DataSource = dbReader
    > CopyResumeTo1.DataValueField = "UserID"
    > CopyResumeTo1.DataTextField = "FullName"
    > CopyResumeTo1.DataBind()
    > CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0"))
    >
    > CopyResumeTo2.DataSource=CopyResumeTo1.Items
    > CopyResumeTo2.DataBind()
    >
    > CopyResumeTo3.DataSource=CopyResumeTo1.Items
    > CopyResumeTo3.DataBind()


    I found what needed to be added to make this work.

    You also need the DataValueField and DataTextField defined, but refering to
    the properties of the ListItem and not the original DataReader. So the
    above needs to be done as so:

    CopyResumeTo1.DataSource = dbReader
    CopyResumeTo1.DataValueField = "UserID"
    CopyResumeTo1.DataTextField = "FullName"
    CopyResumeTo1.DataBind()
    CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0"))

    CopyResumeTo2.DataSource=CopyResumeTo1.Items
    CopyResumeTo2.DataValueField = "Value"
    CopyResumeTo2.DataTextField = "Text"
    CopyResumeTo2.DataBind()

    CopyResumeTo3.DataSource=CopyResumeTo1.Items
    CopyResumeTo3.DataValueField = "Value"
    CopyResumeTo3.DataTextField = "Text"
    CopyResumeTo3.DataBind()

    In DataReader you use the Column name as the source, if you use the
    ListItem - there is no property "UserID" or "FullName". There are 3
    properties - "Selected", "Value" and "Text". In this case, the Value and
    Text properties.

    >
    > The first one (CopyResumeTo1) is working correctly. It has the
    > DataValueField (UserID) as an integer and the DataTextFiled (FullName) as
    > the name and if I look at the ViewSource, it is correct.
    >
    > But when I look at the other 2 dropdowns, it is putting the FullName in
    > both the DataValueField and the DataTextField.
    >
    > Also, I thought my insert would put a 0 in the Value field, but it put a
    > blank there. Is my format incorrect?


    Actually, this format was correct, but I was doing a refresh to check it and
    I was not re-loading the dropdowns, once I did, it worked fine.

    Tom

    >
    > CopyResumeTo1.Items.Insert(0, new ListItem("Select Current User","0")
    >
    > Thanks,
    >
    > Tom
    >>
    >> "tshad" wrote:
    >>
    >>> "timkling" <> wrote in message
    >>> news:D...
    >>> > To bind to a dropdownlist the datasource must implement IListSource or
    >>> > IEnumerable. DropDownList.Items is enumerable.
    >>> >
    >>> > So this will work:
    >>> > ExperienceLevel.DataSource=dbReader
    >>> > ExperienceLevel.DataTextField= "Description"
    >>> > ExperienceLevel.DataValueField="ExperienceLevelID"
    >>> > ExperienceLevel.DataBind()
    >>> > ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >>> > ExperienceLevel.Items(0).value = 0
    >>> >
    >>> > ActualExperienceLevel.DataSource=ExperienceLevel.Items
    >>> > ActualExperienceLevel.DataBind()
    >>> >
    >>>
    >>> Worked great.
    >>>
    >>> > Don't add your default item because it is already in the list.
    >>>
    >>> Which default item - the insert?
    >>>
    >>> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >>>
    >>>
    >>> Thanks,
    >>>
    >>> Tom
    >>> >
    >>> > Tim
    >>> >
    >>> > "tshad" wrote:
    >>> >
    >>> >> Can you databind a dropdownlist to another dropdownlist?
    >>> >>
    >>> >> I have 2 identical list. I am getting my data from a DataReader, so
    >>> >> as
    >>> >> soon
    >>> >> as I have bound the first DDL, I can't do it again to the next as the
    >>> >> dataReader can only be read once.
    >>> >>
    >>> >> I tried:
    >>> >>
    >>> >> ExperienceLevel.DataSource=dbReader
    >>> >> ExperienceLevel.DataTextField= "Description"
    >>> >> ExperienceLevel.DataValueField="ExperienceLevelID"
    >>> >> ExperienceLevel.databind()
    >>> >> ExperienceLevel.Items.Insert(0, "Select Experience Level")
    >>> >> ExperienceLevel.Items(0).value = 0
    >>> >>
    >>> >> ActualExperienceLevel.DataSource=ExperienceLevel
    >>> >> ActualExperienceLevel.DataTextField= DataTextField
    >>> >> ActualExperienceLevel.DataValueField=DataValueField
    >>> >> ActualExperienceLevel.databind()
    >>> >> ActualExperienceLevel.Items.Insert(0, "Select Experience Level")
    >>> >> ActualExperienceLevel.Items(0).value = 0
    >>> >>
    >>> >> But that doesn't seem to work.
    >>> >>
    >>> >> Thanks,
    >>> >>
    >>> >> Tom
    >>> >>
    >>> >>
    >>> >>
    >>>
    >>>
    >>>

    >
    >
    tshad, Oct 19, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,593
    mr2_93
    Oct 2, 2005
  2. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,861
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,294
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  4. jobs
    Replies:
    0
    Views:
    1,618
  5. Earl Teigrob
    Replies:
    1
    Views:
    401
    Shravan
    Mar 1, 2004
Loading...

Share This Page