views:

69

answers:

2

I have GridView bound to some List. In layout I created following template field:

<asp:TemplateField HeaderText="Phrase">
    <ItemTemplate>
        <asp:TextBox ID="phraseTextBox" runat="server" Text='<%# Bind("Phrase") %>'/>
    </ItemTemplate>
</asp:TemplateField>

But after changing the content of any TextBox in column "Phrase" my List doesn't reflect that changes.

What I did wrong?:)

A: 

ItemTemplate will be used only for display/view purpose. You need to have EditItemTemplate to specify how the UI will be while editing the row. Not only that, but you also have to add button/link to the row to push it into an edit mode.

Read this article from MSDN that explains how to edit data using grid-view. Check example under section "Adding Validation Controls to the Editing Interface" for use of EditItemTemplate.

VinayC
Yes, but I need to make the row editable without switching it explicitly. Can we switch mode implicitly when for example user makes my textbox focused?
DixonD
@DixonD, unfortunately GridView is not geared towards such functionality. It allows to edit one row at a time, so it needs event at row level to go into edit mode and then save/cancel button to exit mode.
VinayC
Maybe you can give advice what to use if GridView is not enough to make such functionality?
DixonD
@DixonD - I am not aware of any such built in control. When do you expect the data to be saved? I am assuming that you probably want to edit multiple rows and then save them together. I will probably use Repeater (or ListView) to produce tabular layout. I will probably use html controls (input instead of text-box) and generate unique ids for them using row indexing etc. On save button click, look into Response.Form to get values of those controls and manually save the data into store. This is a quite lot work.
VinayC
A: 

In this line

<asp:TextBox ID="phraseTextBox" runat="server" Text='<%# Bind("Phrase") %>'/>

I'll use Eval and not use Bind

<asp:TextBox ID="phraseTextBox" runat="server" Text='<%# Eval("Phrase") %>'/>

Regards

Juanjobe
I think it should be clear that I need exactly two-way binding.
DixonD