PDA

View Full Version : When to bind with BindingManager



jdcaram
08-27-2006, 03:12 AM
In our system, we have been retrieving objects from the Session and use the DataBindings to set Control Values (TextBoxes, etc.) from the Properties of Objects retrieved from the Session.

But, the IntializeDataBindings happens before the base.OnLoad method, which causes our Properties to fail.

Would there be any issue to have the OnLoad methods for Page and UserControl to call
base.OnLoad(e);
before the
BindingManager.BindSourceToTarget(this, Controller, validationErrors);
method?

Or is there a better way to do what we are trying to do?

Thanks,
Jamie

Here's code example what I'm trying to do:

public partial class TestContainerPage : Spring.Web.UI.Page
{
private TestObject testObject;

protected override void InitializeDataBindings()
{
BindingManager.AddBinding("PageTextBox.Text", "TestName");
}

protected void Page_Load(object sender, System.EventArgs e)
{
object obj = this.Session["MyTestObject"];
if (obj != null)
{
this.testObject = obj as TestObject;
}
}

public string TestName
{
get
{
return this.testObject.Name;
}

set
{
this.testObject.Name = value;
}
}
}

El Gringo
08-30-2006, 04:01 PM
I am not shure what you are trying to achieve. But if you just want your sessionstored objects back in their domain objects before spring assigns them there values, than override the OnLoad() method. Something like this.



public partial class TestContainerPage : Spring.Web.UI.Page
{
private TestObject myObject = new Object();

protected override void InitializeDataBindings()
{
BindingManager.AddBinding("PageTextBox.Text", "MyObject.Name");
}

protected void Page_Load(object sender, System.EventArgs e)
{

Session.Add("MyObject",MyObject);
}
protected override void OnLoad(EventArgs e)
{
if ((User)Session["MyObject"] != null)
{
MyObject = (TestObject)Session["MyObject"];
}
base.OnLoad(e);
}
public string MyObject
{
get
{
return myObject;
}

set
{
this.myObject;
}
}
}

Bruno Baia
08-30-2006, 04:15 PM
Hi,

Have you tried something like that :



protected override void InitializeDataBindings()
{
BindingManager.AddBinding("PageTextBox.Text", "Session['MyTestObject'].Name");
}

Where Session["MyTestObject'] is already set elsewhere.

You can also use ASP.NET injection to set it :
(Don't forget that Session is a Page's property like another one.)


<object id="MyTestObject" type="TestObject, App_Code"/>

<object type="MyPage.aspx">
<property name="Session['MyTestObject']" ref="MyTestObject"/>
</object>



Hope this help,
Bruno