views:

72

answers:

2

Im populating a GridView from List so am forced to use TemplateField controls to allow editing. This requires displaying a TextBox populated with the original value when in edit mode and using FindControl to get the new value out on update submit.

Problem is foundTextBox.Text == "OriginalTextBoxValue"

 <asp:TemplateField HeaderText="A Field">
                    <ItemTemplate>
                        <asp:Label ID="_theLabel" runat="server" Text='<%# Eval("AField") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="_theTextBox" runat="server" Text='<%# Eval("AField") %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

And the code in my update event handler

TextBox newText = (TextBox)_myGridView.Rows[e.RowIndex].FindControl("_thTextBox");
//newText.Text == the old value of the text box
A: 

You've got the code behind in the wrong event handler. Move it to the Editing event handler, so it will populate the textbox whenever the user clicks on the Edit command for a row.

Matthew Jones
+1  A: 

Is your gridview binded at every postback? This could explain why you never get the updated value, because the gridview is rebinded before reading the textbox.

Could you paste your complete update method?

jdecuyper