Results 1 to 3 of 3

Thread: How to create collection of dropdownlist, datatextfield ?

  1. #1
    Join Date
    Nov 2008
    Posts
    89

    How to create collection of dropdownlist, datatextfield ?

    I am attempting to bind data to a dropdownlist and to do that is there a way to make the datatextfield property of a dropdownlist. I know that we can bind the code using the datatextfield. But i have the requirement to bind there First Name as well as last name into that datatextfield, so when the drop down list will be displayed, user should be able to see a Last Name, First Name, rather than just a persons last name,Please suggest some suggestions. Any suggestion would be appreciated.

  2. #2
    Join Date
    Jan 2009
    Posts
    99

    Re: How to create collection of dropdownlist, datatextfield ?

    I think you should declare a method and put he code inside the method.


    DropDownList.DataSource = CreateDataSource();
    DropDownList.DataTextField = "ColumTextField";
    DropDownList.DataValueField = "ColumValueField";

    // Bind the data to the control.
    DropDownList.DataBind();

    It is possible to concatenate your string in your SQL storedprocedure which is much more easier.. or you can do it in your code behind logic functions.This is very easy and this kind of programming logic is mostly being used.

  3. #3
    Join Date
    Jan 2006
    Posts
    211

    Re: How to create collection of dropdownlist, datatextfield ?

    This can be done with the lot of other ways, but the easiest way is to leverage with SQL to do what you want. Essentially you need to create a class that represents binding to a particular UI. So you would map your Model (My Object in your example) to a ViewModel object, and then bind the drop down list that way.

    Code:
    List<MyObject> myList = getObjects();
    ddList.DataSource = (from obj1 in myList
                        select new
                        {
                          pro = obj1.pro,
                          Name = string.Format("{0} ({1})", obj1.Name, obj1.Zip)
                        }).ToList();
    ddList.DataValueField = "pro";
    ddList.DataTextField = "Name";
    ddList.DataBind();

Similar Threads

  1. Using collection in asp.net
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 13-07-2009, 06:21 PM
  2. Display two columns in dropdownlist
    By Zool in forum Software Development
    Replies: 2
    Last Post: 05-06-2009, 02:46 PM
  3. Help to update the DropDownList
    By HiSpeed in forum Software Development
    Replies: 2
    Last Post: 23-04-2009, 11:37 PM
  4. How to select dropdownlist default selected
    By Integer in forum Software Development
    Replies: 5
    Last Post: 18-03-2009, 11:41 PM
  5. Putting a dropdownlist in the column header of a GridView
    By ShonaliB in forum Software Development
    Replies: 4
    Last Post: 10-02-2009, 06:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,283,800.71072 seconds with 17 queries