Let’s say we have a speaker database and we wants to bind the name of the speakers with the DropDownList. First placed an ASP.NET Dropdown control with the page and set the “DataTextField” and “DataValueField” properties.
We can set the ddlName.DataSource to specifying the data source from the code behind and bind the data with dropdpwnlist, but in this case from the code behind to providing the data source.
Now, instead of specifying the DataSource, we will be setting the Dropdownlists SelectMethod property to point a method GetSpeakerNames() within the code-behind file.
Select method is expected to return us result of type IQueryable<TYPE>. Here is GetSpeakerName() method is defined as follows.
/// <summary> /// Return the Speakers Name /// </summary> /// <returns></returns> public IQueryable<Speaker> GetSpeakerNames() { DeveloperConferenceDBEntities datasource = new DeveloperConferenceDBEntities(); return datasource.Speakers; }So, Instead of specifying the data source we are specifying the SelectMethod, which return the IQueryable type of Speaker object. Run the application, you will find the names binded with dropdown list.
Hope this helps !
No comments:
Post a Comment