Duncan
Duncan

Reputation: 1798

FindControl("someTextBox") in GridView not sending the updated value

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

Upvotes: 1

Views: 4844

Answers (2)

jdecuyper
jdecuyper

Reputation: 3963

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?

Upvotes: 4

Matthew Jones
Matthew Jones

Reputation: 26190

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.

Upvotes: 0

Related Questions