Prashant
Prashant

Reputation: 15

asp.net FormView Data Binding with a Collection of Data

I have a collection of records, each record has an ID and a description. Now in my formview I have 8 textboxes and I want each text box to hold description of each record.

So if I do this

Text='<%# Eval("Record[0].Description") %>' />

This gives an error, any other way to do it?

Also can I do it in the markup, or do I need to do it in code behind, under databound method for the formview?

Thanks..

Upvotes: 0

Views: 1436

Answers (2)

user2354869
user2354869

Reputation:

FormView is not meant for showing List of Data.

If you have a List of Data, then you should use GridView or ListView.

Bind your FormView with a datasource having single record and then directly Eval the fields of the datasource.

i.e. do this:

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSourceId">  
    <ItemTemplate>
        <asp:TextBox id="txtDescription" 
                     Text='<%# Eval("Description") %>' />

        <asp:TextBox id="txtName" 
                     Text='<%# Eval("Name") %>' />


         ..
     </ItemTemplate>
 </asp:FormView>

so basically, your FormView should contain different DataField and it should be bound to a DataSource having just one Item.

Upvotes: 1

Kenneth
Kenneth

Reputation: 28737

You could use a repeater inside:

<asp:repeater ID="rep" runat="server" DataSource='<%# Eval("Record") &>'>
    <ItemTemplate>
        <asp:textbox id="txt" runat="server" Text='<%# Eval("Description") &>' />
    </ItemTemplate>
</asp:repeater>

In the repeater you will bind to your outer datasource, inside the repeater your datacontext is the record

Upvotes: 0

Related Questions