开发者

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

开发者 https://www.devze.com 2022-12-10 05:44 出处:网络
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 FindCon

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开发者_Python百科.

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


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消