views:

100

answers:

1

I'm setting up a web application with multiple forms. Each form is defined within an asp:FormView with the DataSource set to an ObjectDataSource. Each form contains it's own set of fields and always contains one or more blocks of fields, which are the same for multiple forms.

Because this blocks are the same, I decided to define them in a custom usercontrol. The questions that came up with this:

  1. How can I use the same datasource for the input fields in the usercontrol as in the 'higher' asp:FormView?
  2. Is it possible to use DataBinding.Bind() for the input fields in the usercontrol, with this same datasource?

Thanks in advance for replies.

+1  A: 

After a long search, I found a similar problem on this website: http://weblogs.asp.net/anasghanem/archive/2009/03/31/sharing-formview-edit-and-insert-templates-and-avoid-duplicate-markup.aspx.

In short the solution hierarchy:

<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" DataSourceID="ObjectDataSource1">
    <uc1:TestControl ID="TestControl1" runat="server" PhoneNumber='<%# Bind("PhoneNumber") %>' />

And the codebehind for the usercontrol:

[Bindable(true)]
public partial class TestControl : System.Web.UI.UserControl
{
    [Bindable(true), DefaultValue("")]
    public string PhoneNumber
    {
        get
        {
            return this.PhoneTextBox.Text;
        }
        set
        {
            this.PhoneTextBox.Text = value;
        }
    }
}
Monty