How To Use Bound ComboBox Inside DetailsView

Aug 19, 2008 at 1:19 AM
Edited Aug 19, 2008 at 1:37 AM
Apparently, the comboBox (and maybe other controls of this collection) will not behave properly in a DetailsView in the following scenario. I'm no expert on this, so maybe else will see my mistake.

You create a details view bound to a dataSource, enable editing, and convert one field into a template. Then you add a comboBox to the editItem template, and bind that comboBox to a different dataSource.

Then, to bind the comboBox's text value to the current record, you add the following attribute to the asp control. This attribute can be copied from the default textBox that visual studio created when you converted the field into a template:

  Text='<%# Bind("Region") %>'
So the resulting script will look like:
<mb:ComboBox ID="ComboBox2" runat="server" Text='<%# Bind("Region") %>' DataSourceID="SqlDataSource1"
    DataTextField="Region" DataValueField="Region">

When you run the page, and click "Edit", you'll get an error something like "There is no source code available for the current location" in debug mode, or "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

The solution I suggest is the following:

Remove the following line from the combobox:

 Text='<%# Bind("Region") %>'

Next, add the following Prerender event to the DetailsView:

protected void DetailsView1_PreRender(object sender, EventArgs e)
        if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            ComboBox boxRegion = DetailsView1.FindControl("ComboBox1");
            boxRegion.Text=     // here's where you have to get the bound value of this field for the current record in the details view
I'll leave it to someone else to fill i n the missing bit of code above.
The purpose is to use code to load the current value of the field into the comboBox when the user switches to edit mode.
The idea for this solution came from:

Three questions:
  • is it possible to use the comboBox as a databound control in a detailsView template field without any custom event code?
  • if not, what should the missing code above look like?
  • would it be possible, rather than writing to the PreRender event, to write a custom function to replace the following?
 Text='<%# Bind("Region") %>'
with something like:
Text='<%# MyBind("Region") %>'
If so, how and where?
Nov 25, 2008 at 9:07 AM
I have this same problem.
Selectedvalue parameter doesn't work. And i can't get the source code work properly to correct the by myself.
Does anyone have fixed combobox control? one with working selectedvalue or binding ?

thanks in advance