【发布时间】:2015-06-08 05:00:39
【问题描述】:
我无法检索在 GridView 的文本框模板字段中输入的新值。
这是我的标记:
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
这就是我在 GridView 的 RowCommand 事件处理程序中尝试检索新值的方式:
string userName = ((TextBox)grdUserList.Rows[rowIndex].FindControl("txtUserName")).Text;
当我执行这段代码时,我得到的是旧值而不是新输入的值。
有人知道我错过了什么吗?提前致谢。
【问题讨论】:
-
你能告诉我们你绑定gridview的代码吗?
-
@Adil 嗨。我刚刚找到了解决我的问题的方法。我搜索并发现在检索过程开始之前正在刷新 GridView,因为我在 Page_Load 方法上重新绑定了 GridView。我通过在回发时(或至少在我进行更改之前)不重新绑定gridview来解决问题。无论如何感谢您的回复:)
-
我以为您在回发时绑定了 GridView 并丢失了新输入的值。
-
@Adil 我在 Page_Load 中绑定了 gridview。当页面回发时,我不知道 pageLoad 是在 GridView 的 RowCommand 事件之前先执行的。所以我认为发生的事情是gridview的内容(包含新数据)被我的数据库中的原始内容覆盖(执行Page_Load时)所以当我试图在Row_Command处理程序中获取新数据时,我得到了原始内容。请原谅我的英语:D
标签: c# asp.net gridview templatefield