This is a migrated thread and some comments may be shown as answers.

Cleanest Way to Bind Non-Datatable Source to DropDownList in Code Behind

6 Answers 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shira
Top achievements
Rank 1
Shira asked on 15 Apr 2011, 11:45 PM
I have
  1. A couple of dropdown lists in my RadGrid
  2. A multilingual application, such that the ViewModel provides an object for each property, but getting the display text requires a translation step to the user's language - can't just display the ViewModel property directly.  I guess I COULD change this to have one property on the ViewModel that returns the string display value and another that returns the integer underlying value, but that seems messy to me - would much prefer a solution like IValueConverter to convert between the ViewModel and the displayed value based on the type of the property.  Is that the "best practice"?  Will that cause extra server round-trips, because the property that the client side form binds to requires server side logic to execute in order to produce its value?
  3. A form template for my EditForm

I want to have a clean code-behind that uses binding to populate the list values and the selected index in the dropdown lists in both the MasterTableView and the Edit Form's FormTemplate.

I can make it work without adding the strings and integer fields to the ViewModel, but the code behind is extensive and messy.  I can't figure out any clean way to do it.  

I looked at the GridDropDownColumn control, but found that it's supposed only to have datatables underlying it (http://www.telerik.com/community/forums/aspnet/grid/populating-griddropdowncolumn-from-code-behind.aspx), and only found examples of it being bound to a source whose connection string etc. were declared in the .aspx (not in the code behind).  I'm retrieving my values via Entity Framework and then a subsequent manipulation to acquire the values for the correct language, so that doesn't work for me.

I can't declaratively specify the options for the dropdown list in the .aspx due to the need to translate them, but if I don't declare them in the XAML, then I can't bind the selected value, because it finds that the corresponding list item doesn't exist.  I also can't even bind the selected value in the case of the MasterTableView's view mode entry because of the need to translate the text to be displayed using a function that's called server side.

As-is, in ItemDataBound, for EACH dropdown field in my form, there's code to:
  1. Populate dropdown values list manually in Edit Form (requires casting the bound item, getting the list of values with translations, and manually creating ListItems
  2. Setting selected item in Edit Form (reading the value from the underlying data source item and using .FindControl to get the control that I need to update)
  3. Setting default value for the dropdown list in the Insert form
  4. Set value for the field in the MasterTableView (again doing the translation)

Seems like a lot of code to have for each field.  Seems like there should be some point BEFORE databinding where I can set the list items in the dropdownlist control, so that the databinding can go through successfully.  Just because I need to set the list items in code behind, why should I ALSO be forced to set the selected item there?

Is there a better way?

Thanks!




6 Answers, 1 is accepted

Sort by
0
Bernie
Top achievements
Rank 1
answered on 16 Apr 2011, 04:57 AM
I am also VERY keen to hear the answer to this.
B
0
Marin
Telerik team
answered on 19 Apr 2011, 02:42 PM
Hello,

I am also posting the response here for the benefit of the community.
If you do not mind we will continue any further communication in the support ticket referencing this forum thread.

The ValueConverters are a feature of the binding in the Silverlight / WPF solutions. The ASP.NET AJAX Framework does not provide such utilities out of the box. If you do need to operate with different languages in your application another option is to use the ASP.NET Localization features of the framework.
Basically there are two ways to bind the combo box in the GridDropDownColumn of the grid. The first, and simpler one is binding in the markup of the aspx page to some datasource control, and the second one is binding manually the combo box server side. Indeed this approach requires some code to be written server side but this is because you manually have to set the respective datasource and the selected value of the combo. Actually binding in the ItemDataBound event is the recommended way to do it as described in this help topic. Yet you need to:
1) retrieve the combo box control when the item is in edit mode
2) set its datasource to a collection of objects holding the integer and string values as configured in the       grid column
3) call databind of the combo
4) set its SelectedValue property to the respective value from the grid item

Similar code is executed internally by the framework when you have bound the controls in the markup, so the only overhead in your case is the translation of the text which you can try to overcome using localization.

Regards,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Shira
Top achievements
Rank 1
answered on 19 Apr 2011, 11:31 PM
Glad to know, but I find the reply disappointing.  I hope in future releases Telerik will consider a cleaner model that allows for declarative binding to be used even when the list items need to be defined in code behind, and that allows for declarative setup of transformations that will occur to the data (multilingual translations in cases where ASP.NET localization is not used is only one of many, many, many use cases for this), modelled after the IValueConverter interface.  For now, I've had to implement my own custom class for each dropdown control to keep the logic encapsulated.

Thanks.

0
Marin
Telerik team
answered on 20 Apr 2011, 04:48 PM
Hi Shira,

Thank you for the valuable feedback. We appreciate your suggestions.

Greetings,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Shira
Top achievements
Rank 1
answered on 03 Jun 2011, 10:38 PM
Looks from another post like this may be an alternative:

<asp:DropDownList ID="myDropdown" runat="server" AppendDataBoundItems="True" onLoad="myDropDown_Load" DataTextField="myDisplayField" DataValueField="myValueField" SelectedValue='<%# Bind("myTableColumn") %>'
<asp:ListItem Text="" Value=""></asp:ListItem> 
</asp:DropDownList> 
 
... and in the code behind... 
 
    protected void myDropDown_Load(object sender, EventArgs e) 
    { 
        IEnumerable<myType> tempSource = MethodToCreateTheList(); 
        DropDownList tempList = (DropDownList) sender; 
        tempList.DataSource = tempSource
    } 
 
Here's the full post thread: http://www.telerik.com/community/forums/aspnet/grid/assign-current-value-to-textbox-when-in-editmode.aspx
0
Abisek
Top achievements
Rank 1
answered on 01 Jun 2012, 01:26 PM
.
Tags
Grid
Asked by
Shira
Top achievements
Rank 1
Answers by
Bernie
Top achievements
Rank 1
Marin
Telerik team
Shira
Top achievements
Rank 1
Abisek
Top achievements
Rank 1
Share this question
or