locked
Want to Bind an Object with the ASP.Net Control RRS feed

  • Question

  • User1624688223 posted

    Hi,

    I want to bind my ASP.Net control to an object. But your answer would be object datasource. 

    But, I want like this.

    For example, if i have class called Person and in that I have two Properties Name and Age.  This Object's Property should be binded with the asp:Textbox called txtName.  This means if I pass the value to the Object's Property it should get automatically set to the txtName.Text.

    Example:

    If I pass

    PersonObject.Name = "XXXX";

     Then in my textbox txtName, I should view that as XXXX. 

    Can anyone help me in this regards.

    Advanced Thanks. 

     

     

     

     

     

     

    Thursday, April 16, 2009 2:03 AM

Answers

  • User-989915499 posted

    Hi There,

     I don't think you can do that, unless there is new feature that i haven't notice.

    Most of the time you will need to get value back from the control you bind to.

    Unless you create your own custom control (FormViewTemplate) create function bind the object to it, and return the object back to you.

    Hope it helps!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 19, 2009 7:20 PM

All replies

  • User-989915499 posted

    Hi There,

    You can do it quick and easy way. by new an array of object and and your object to array then bind it to your control container.

    Example:

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)

    {

    Person person = new Person();person.Name = "xxxx";

    person.Age = 18;

    FormView1.DataSource =
    new object[] { person };

    FormView1.DataBind();

    }

     

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

     

    <asp:FormView ID="FormView1" runat="server">

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text=' <%# Bind("Name") %>'></asp:Label>

    <br />

    <asp:Label ID="Label2" runat="server" Text=' <%# Bind("Age") %>'></asp:Label>

    </ItemTemplate>

    </asp:FormView>

     

    </div>

    </form>

    </body>

    </html>

    You could also use collection, list and more

    Cheers,

    Thursday, April 16, 2009 2:33 AM
  • User1624688223 posted

    Hi, Thanks for the reply.

    But, if i want retrieve the value back from the same control, then I should not use the control again.

    I mean, I should not use txtName.Text again for retrieving the value.

    I should use only PropertyObject.Name for retrieving the value.

    Please help me in this regard. 

     

     

    Thursday, April 16, 2009 3:19 AM
  • User-989915499 posted

    Hi There,

     I don't think you can do that, unless there is new feature that i haven't notice.

    Most of the time you will need to get value back from the control you bind to.

    Unless you create your own custom control (FormViewTemplate) create function bind the object to it, and return the object back to you.

    Hope it helps!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 19, 2009 7:20 PM