Results 1 to 7 of 7

Thread: DropDownList and BindingManger

  1. #1
    Join Date
    Sep 2006
    Posts
    12

    Default DropDownList and BindingManger

    Hi,
    I have a web page (extending SPring.Web.Page) that displays a drop down list with the following code:

    ...

    protected override void OnInitializeControls(EventArgs e)
    {

    if (!IsPostBack)
    BindLocations();
    }


    protected override void InitializeDataBindings()
    {
    base.InitializeDataBindings();
    BindingManager.AddBinding("ddlLocations.SelectedVa lue", "SearchCriteria.LocationCode");
    }

    private void BindLocations()
    {
    ddlLocations.DataSource = PageController.GetAllLocations();
    ddlLocations.DataTextField = "Name";
    ddlLocations.DataValueField = "Identity";
    ddlLocations.DataBind();
    ddlLocations.Items.Insert(0, DropDownUtils.UnselectedItem);
    }
    ....
    The data source is kept in the controller, which is kept in the session.

    This works fine the first time I select a value and navigate somewhere else.

    However, the second time I select a value and press ''Go', I get the following error :

    "Cannot have multiple items selected in a DropDownList."


    It seems that the BIndingManager should call 'ClearSelection' on the drop down list before binding from the model to the ddl.


    Am I using the wrong syntax for this? Is there a workaround I could use?

    Thanks very much
    Sebastian

  2. #2
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    Hi,

    I'm afraid in this case it's your job to call ClearSelection() on the ddl. DataBindingManager is not aware of the ddl. It just copies property values from source to target and vice versa.

    Try overriding OnPreRender() like this:

    Code:
    void OnPreRender( EventArgs e )
    {
      ddl.ClearSelection();
      base.OnPreRender(); // DataBinding is done here!
    }
    cheers,
    Erich

  3. #3
    Join Date
    Oct 2005
    Location
    Toulouse, France
    Posts
    1,407

    Default

    Hi,

    another tricky solution when you know all the possibilities of the expression framework is something like this :

    Code:
    protected override void InitializeDataBindings()
    {
       base.InitializeDataBindings();
       BindingManager.AddBinding("ddlLocations.SelectedValue", "SearchCriteria.LocationCode", BindingDirection.SourceToTarget);
       BindingManager.AddBinding("{ddlLocations.ClearSelection();ddlLocations}.SelectedValue", "SearchCriteria.LocationCode", BindingDirection.TargetToSource);
    }
    The first binds from the ddl.SelectedValue to the model.
    The second call ddl.ClearSelection() before to bind from the model to the ddl.SelectedValue.

    -Bruno
    Last edited by Bruno Baia; 09-27-2006 at 11:39 PM.
    My english is as poor as my taylor is rich

  4. #4
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    Nice idea - I didn't think of this ;-)

    cheers,
    Erich

  5. #5
    Join Date
    Sep 2006
    Posts
    12

    Default

    Hi,
    Thanks a lot to both for your time and dedication!
    I've tried both approaches, but in the two cases the same error is still happening.

    I've stepped into Spring code and was able to see the 'SelectedValue' property of the ddl being correctly set at 'SetPropertyOrFieldValueInternal' method.

    Also, I confirmed that at this point the ddl had no item selected (inspected each of them and saw the 'Selected' property beinf 'false') after the property was set , that is, after the line with the code...

    pi.SetValue(context, newValue, null);


    at the PropertyOrFIeldNode class, the ddl had the right item selected.

    However, the same httpexception is being thrown by the web server at the point of render.

    It seems I'll need to bind the ddl manually.

    Thank you again,

    Sebastian

  6. #6
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    Hi Sebastian,

    could you please post your aspx + codebhind (as attachment)? I would like to investigate this - if it's a spring-problem, we need to fix it.

    at the moment I have no idea for this behaviour.

    tia,
    Erich

  7. #7
    Join Date
    Sep 2006
    Posts
    12

    Default

    Hi Erich,

    I just found it was a problem with my code.

    Basically, I added a 'dummy' entry to all ddls in the page ('--SELECT--' item)

    I didn't realise this item was shared rather than being a diff instance per ddl.

    Thereforem if it was selected in one ddl, it was selected in all of them, resulting in multiple selections when a ddl had a real selection.

    I changed this and everything works fine. There is no need for calling 'ClearSelection' at all: using the default Bidirectional Binding works fine for all dlls.

    Sorry about that.

    Sebastian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •