How do I add multiple items inside a dropdownlist using objectdatasource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    How do I add multiple items inside a dropdownlist using objectdatasource

    Hi,

    I have used a object datasource and have populated the a field named Titles in dropdownlist.

    Now I want Titles as well as AuthorName to be shown in the dropdown list. as

    Hound of baskervilles/Sherlock holmes.



    How do i do that in objectdatasourc e. Pls help

    Regards

    cmrhema
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Wouldn't it be possible to bind the Dropdown to a List<string> instead and then add the strings in the collection in the format that you want?

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Your ObjectDataSourc e will have a property that the DropDownList will be bound to.

      All you have to do is edit this property and make sure that it returns what you want to display.

      Comment

      • cmrhema
        Contributor
        • Jan 2007
        • 375

        #4
        Originally posted by r035198x
        Wouldn't it be possible to bind the Dropdown to a List<string> instead and then add the strings in the collection in the format that you want?
        Yes I did the same and it worked.
        Thnx

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Hmmm somehow I answered your question (thinking it was you) when I was explaining things to someone else with a similar problem.

          If you're still curious about using an ObjectDataSourc e instead of a List Of Strings check out this post :)

          Comment

          • cmrhema
            Contributor
            • Jan 2007
            • 375

            #6
            Originally posted by Frinavale
            Hmmm somehow I answered your question (thinking it was you) when I was explaining things to someone else with a similar problem.

            If you're still curious about using an ObjectDataSourc e instead of a List Of Strings check out this post :)
            Yes, Actually I wanted a initially to display only titles, so I first went for ObjectDataSourc e(ODS), but then I wanted concatenation of title/author, so went for generic list, which I was partially aware of and did this way

            And I concatenated as
            Code:
            ddlKeys.DataTextField = "KeyValue ";
            Infact I call a method where I populate the generic list as below
            Code:
              private void TGenerateKeys<T>(SqlDataReader returnData, ref List<Keys> KeysList)
                    {
                        while (returnData.Read())
                        {
                            Keys oKeys = new Keys();
                            oKeys.KeyValue = Convert.ToString(returnData["TITLE"]) + "/" + Convert.ToString(returnData["AUTHOR"]);
                           
                            KeysList.Add(oKeys);
                        }
                    }
            But although I have not implemented, I think, while retrieving query from sql server, I can put something as
            select title+author as "Both" from tablename
            and directly bind this value to ODS.

            Of course I can bind the "KeyValue", field too, but I had one more work, I had to include a value called as "All", so if the user chooses "All", I should be able to display all the results.
            If I add it through properties (I mean press F4 on dropdownlist), then although I select other titles, the dropdownlist always showed "All".(beca use I gave selected =true in dropdownlist)

            I thought it would be easier if I dynamically populate the dropdownlist and after binding to the above list. I followed this code

            Code:
             List<Keys> olistkeys=Keys.GetKeysByEmailId(txtEmail.Text);
                         ddlKeys.DataSource = olistkeys;
                         ddlKeys.DataTextField = "keyvalue";
                         ddlKeys.DataBind();
            and later added the "All" as
            Code:
            ddlKeys.Items.Add(new ListItem("All", "0"));
            And resolved my issues.

            Sorry for dragging this to such a length, and pls excuse my English.

            Regards
            cmrhema

            Comment

            Working...