Binding custom object to formview

Discussion in 'ASP .Net' started by Guest, Feb 19, 2007.

  1. Guest

    Guest Guest

    Hi,

    I have an object, for example User. User contains various properties which i
    have been able to bind to successfully using wizards and the form view.
    However if the class User has a property which is not a string, for example
    a custom type Address which contains properties such as StreetAddress, City,
    County, Country etc how do i bind to those properties on the same form view?

    I have tried doing Bind("Address.StreetAddress") but this just raises a
    compile error?

    Thanks for your help

    Ash
     
    Guest, Feb 19, 2007
    #1
    1. Advertisements

  2. Guest

    sloan Guest

    Me.txtOne.DataBindings.Add(New Binding("Text", Me.m_user,
    "Address.StreetAddress"))
    Me.txtTwo.DataBindings.Add(New Binding("Text", Me.m_user,
    "Address.StreetAddress"))

    try this.

    it works in winforms. not sure about asp.net off hand.
     
    sloan, Feb 19, 2007
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Hi Ash,

    Bind() method does not support complex binding expressions (i.e. set a
    property of object-type property as you presented), but Eval() does.
    Unfortunatelly, Eval() can only be used to evaluate expression when data is
    bound (one way), and not to read changed value back. So you if go for Eval,
    you will end up handling FormView's events to set insertparameters or
    updateparanmeters manually.
     
    Guest, Feb 19, 2007
    #3
  4. Guest

    sloan Guest

    If it works , please post a response.

    I'd like to know myself.
     
    sloan, Feb 19, 2007
    #4
  5. Guest

    Guest Guest

    Hi thanks for the information,

    What about a GridView i have it displaying objects but if one of the
    properties is an object itself of which i need a property of how would i
    access that? for example below i BillingType is an object but I need to
    access a property of that object?

    <asp:BoundField DataField="BillingType" HeaderText="Billing Type"
    SortExpression="BillingType" />

    Thanks for your help
     
    Guest, Feb 20, 2007
    #5
  6. Guest

    Guest Guest

    Hi there,

    I think complex data binding also does not work for BoundField, you have to
    use templatefield with Eval("Property.AnotherProperty")
     
    Guest, Feb 20, 2007
    #6
  7. Guest

    Guest Guest

    Thanks for your responses they have been great help.

    i have got everything to work so far but i am a bit confused as to where i
    should put the event handeling you were discussing earlier about the update
    and insert parameters when using Eval?

    "So you if go for Eval, you will end up handling FormView's events to set
    insertparameters or updateparanmeters manually.
    "
    How do i make sure that my code only gets executed not the formviews default
    handeling?

    Thanks
     
    Guest, Feb 20, 2007
    #7
  8. Guest

    Guest Guest

    Howdy,

    Sorry, i meant handling objectdatasource events, of course if you use
    formview in conjunction with any datasource control, if not i can't really
    see the posint of using FormView for edit/insert operations. I prepared a
    simple example:

    -- begin aspx code --
    <script runat="server">

    private void SetPersonInaccessibleDetails(Person person)
    {
    if (person == null)
    throw new NullReferenceException("person");

    TextBox textBox = (TextBox)fv.FindControl("FirstNameTextBox");
    person.Name.FirstName = textBox.Text;

    textBox = (TextBox)fv.FindControl("LastNameTextBox");
    person.Name.LastName = textBox.Text;
    }


    protected void ds_Inserting(object sender, ObjectDataSourceMethodEventArgs
    e)
    {
    SetPersonInaccessibleDetails((Person)e.InputParameters[0]);
    }

    protected void ds_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
    SetPersonInaccessibleDetails((Person)e.InputParameters[0]);
    }


    </script>

    <asp:ObjectDataSource runat="server" ID="ds"
    TypeName="PersonManager"
    SelectMethod="Get" DataObjectTypeName="Person"
    UpdateMethod="Update" InsertMethod="Insert"
    OnInserting="ds_Inserting" OnUpdating="ds_Updating">
    <SelectParameters>
    <asp:parameter Name="id" Type="Int32" />
    </SelectParameters>
    </asp:ObjectDataSource>

    <asp:FormView runat="server" ID="fv"
    DefaultMode="Insert"
    DataSourceID="ds"
    DataKeyNames="Id">
    <InsertItemTemplate>
    Age:
    <asp:TextBox ID="AgeTextBox" runat="server"
    Text='<%# Bind("Age") %>'>
    </asp:TextBox><br />
    First Name:
    <asp:TextBox ID="FirstNameTextBox" runat="server">
    </asp:TextBox><br />
    Last Name:
    <asp:TextBox ID="LastNameTextBox" runat="server">
    </asp:TextBox><br />
    <asp:LinkButton ID="InsertButton" runat="server"
    CausesValidation="True"
    CommandName="Insert" Text="Insert">
    </asp:LinkButton>
    <asp:LinkButton ID="InsertCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel"
    Text="Cancel">
    </asp:LinkButton>
    </InsertItemTemplate>
    <EditItemTemplate>
    Age:
    <asp:TextBox ID="AgeTextBox" runat="server"
    Text='<%# Bind("Age") %>'>
    </asp:TextBox><br />
    First Name:
    <asp:TextBox ID="FirstNameTextBox" runat="server"
    Text='<%# Eval("Name.FirstName") %>'>
    </asp:TextBox><br />
    Last Name:
    <asp:TextBox ID="LastNameTextBox" runat="server"
    Text='<%# Eval("Name.LastName") %>'>
    </asp:TextBox><br />
    <asp:LinkButton ID="UpdateButton" runat="server"
    CausesValidation="True" CommandName="Update" Text="Insert">
    </asp:LinkButton>
    <asp:LinkButton ID="UpdateCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel"
    Text="Cancel">
    </asp:LinkButton>
    </EditItemTemplate>
    <ItemTemplate>
    Id:
    <asp:Label ID="IdLabel" runat="server"
    Text='<%# Eval("Id") %>'></asp:Label><br />
    Age:
    <asp:Label ID="AgeLabel" runat="server"
    Text='<%# Eval("Age") %>'></asp:Label><br />
    Name:
    <asp:Label ID="NameLabel" runat="server"
    Text='<%# Eval("Name.FirstName") %>'/><br />
    <asp:LinkButton ID="NewButton" runat="server"
    CausesValidation="False" CommandName="New"
    Text="New">
    </asp:LinkButton>
    </ItemTemplate>
    </asp:FormView>

    -- end aspx code --

    -- begin c# code (separate file with pesudo: D business object --


    -- end c# code --

    /// <summary>
    /// Summary description for ProductsBLL
    /// </summary>
    public class PersonManager
    {

    public void Update(Person person)
    {
    }

    public void Insert(Person person)
    {
    //
    }

    private Random random = new Random();
    public Person Get(int id)
    {
    string str = id.ToString();

    Person person = new Person();

    person.Id = id;
    person.Age = this.random.Next(1, 100);
    person.Name.FirstName = "FirstName" + str;
    person.Name.LastName = "LastName" + str;

    return person;
    }
    }
    [Serializable]
    public class Person
    {

    private int id;
    /// <summary>
    ///
    /// </summary>
    public int Id
    {
    get
    {
    return this.id;
    }
    set
    {
    this.id = value;
    }
    }


    private FullName fullName = new FullName();
    public FullName Name
    {
    get
    {
    return this.fullName;
    }
    }

    private int age = 18;
    /// <summary>
    ///
    /// </summary>
    public int Age
    {
    get
    {
    return this.age;
    }
    set
    {
    if (value < 0)
    throw new ArgumentOutOfRangeException();
    this.age = value;
    }
    }

    }

    public class FullName
    {
    public FullName()
    {
    }

    private string firstName;
    /// <summary>
    ///
    /// </summary>
    public string FirstName
    {
    get
    {
    return this.firstName == null ?
    String.Empty : this.lastName;
    }
    set
    {
    this.firstName = value;
    }
    }

    private string lastName;
    /// <summary>
    ///
    /// </summary>
    public string LastName
    {
    get
    {
    return this.lastName == null ?
    String.Empty : this.lastName;
    }
    set
    {
    this.lastName = value;
    }
    }
    }
     
    Guest, Feb 20, 2007
    #8
  9. Guest

    Guest Guest

    Thanks works great!

     
    Guest, Feb 20, 2007
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.