Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I add multiple items inside a dropdownlist using objectdatasource

Needs Regular Fix
 
Join Date: Jan 2007
Posts: 365
#1: Jun 2 '09
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 objectdatasource. Pls help

Regards

cmrhema

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 2 '09

re: How do I add multiple items inside a dropdownlist using objectdatasource


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?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#3: Jun 3 '09

re: How do I add multiple items inside a dropdownlist using objectdatasource


Your ObjectDataSource 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.
Needs Regular Fix
 
Join Date: Jan 2007
Posts: 365
#4: Jun 4 '09

re: How do I add multiple items inside a dropdownlist using objectdatasource


Quote:

Originally Posted by r035198x View Post

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
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#5: Jun 4 '09

re: How do I add multiple items inside a dropdownlist using objectdatasource


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 ObjectDataSource instead of a List Of Strings check out this post :)
Needs Regular Fix
 
Join Date: Jan 2007
Posts: 365
#6: Jun 5 '09

re: How do I add multiple items inside a dropdownlist using objectdatasource


Quote:

Originally Posted by Frinavale View Post

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 ObjectDataSource 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 ObjectDataSource(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
Expand|Select|Wrap|Line Numbers
  1. ddlKeys.DataTextField = "KeyValue ";
Infact I call a method where I populate the generic list as below
Expand|Select|Wrap|Line Numbers
  1.   private void TGenerateKeys<T>(SqlDataReader returnData, ref List<Keys> KeysList)
  2.         {
  3.             while (returnData.Read())
  4.             {
  5.                 Keys oKeys = new Keys();
  6.                 oKeys.KeyValue = Convert.ToString(returnData["TITLE"]) + "/" + Convert.ToString(returnData["AUTHOR"]);
  7.  
  8.                 KeysList.Add(oKeys);
  9.             }
  10.         }
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".(because 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

Expand|Select|Wrap|Line Numbers
  1.  List<Keys> olistkeys=Keys.GetKeysByEmailId(txtEmail.Text);
  2.              ddlKeys.DataSource = olistkeys;
  3.              ddlKeys.DataTextField = "keyvalue";
  4.              ddlKeys.DataBind();
and later added the "All" as
Expand|Select|Wrap|Line Numbers
  1. 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
Reply