1

Lets say I have an object

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

And that object is retrieved from a Factory (ie Can't use SQLDataSource or anything like that)

Person person = PersonFactory.GetPerson();

How can I two-way DataBind the two properties to Textboxes on a web form? I looked into FormView, but that doesn't seem to fit my needs as I am not iterating over a collection of objects. And when I tried to use it, I don't seem to be getting the posted values in the Person object in the FormUpdated event. And I am binding like this

Markup

<asp:Textbox Text=<%# Bind("Name") %> />

Code behind

 FormView1.DataSource = new List() { person };
 FormView1.DataBind();

I feel like I am missing something really obvious. Should I be using a FormView? It doesn't seem like it a proper fit for simple data binding, but the <#% Bind %> method must be in some type of container -- is there a more suitable object?

1
  • I am using an update panel if that makes any difference Commented Jul 30, 2009 at 18:25

3 Answers 3

1

You need to handle the updates to your FormView - the updates in the asp.net databound controls are not automatic. I'd also consider using an ObjectDataSource - keeping your binding all in the markup can make things easier to find. When you use the ObjectDataSource - it'll automatically wrap your single object in an IEnumerable, so binding to a method that returns a Person is acceptable. You could also consider using a DetailsView if you don't want to write out the form yoruself. In your case, you could do the following

<asp:FormView runat="server" DataSourceID="MyPersonDataSource"> ... </asp:FormView>
<asp:ObjectDataSource runat="server" ID="MyPersonDataSource" 
    TypeName="PersonFactory" DataObjectTypeName="Person" 
    SelectMethod="GetPerson" UpdateMethod="UpdatePerson" />

And to facilitate this, you'd need an UpdateMethod(Person) method on your PersonFactory class. Doing it this way eliminates your binding from the codebehind, and will allow your updates to your person object to be persisted to your data store without you having to handle the update events yourself.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help, is it possible to avoid the use of the object data source?
Sure, you'd have to handle the FormView's ItemUpdated event manually though. ODS wraps that all up for you nicely.
Damn, My factory comes from a spring proxy class, I am having trouble getting the ObjectDataSource to take that in
make sure you have the full namespace & class name for your factory in the TypeName prop, and the factory method in the SelectMethod property of the ODS. The DataObjectTypeName should be the full namespace and name of the Person class, and the UpdateMethod should be the method on your factory that takes a single person as a parameter.
Ahh, it is because I was using a generic type, It didn't dawn on me that you need to specify the CLR tpye name msdn.microsoft.com/en-us/library/w3f99sx1.aspx
1

Try calling the DataBind method on your TextBox controls.

protected override void OnLoad(EventArgs e)
{
     base.OnLoad(e);

     MyTextBox1.DataBind();
     MyTextBox2.DataBind();
}

I've never tried doing two way binding in exactly this way before, but using the Bind("property") syntax, it should work this way as far as I know.

If calling the DataBind method doesn't work, then the FormView is your best bet.

4 Comments

The textboxes need to be in a container to use binding. I am using the FormView right now, but I can't seem to get my posted values to reflect in my object
I guess it would have to be in a container. How are you checking the values on postback? Now that I think about it a little more I think you have to use a data source of some kind because that's what it will pass back the changes too.
I am setting the datasource in the code behind, I will update my post.
The DataBind method won't help with the 2 way binding - that's just for telling controls to rebind themselves to the underlying data. You still need to handle the updates on your own.
-1
DataBinder.Eval(Container.DataItem, "Name")

This should do the trick, but I think you need some event-handling of your own to do the 2-way binding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.