简体   繁体   English

如何在DataGrid中检索数据绑定文本框的更改值

[英]How to retrieve a changed value of databound textbox within datagrid

ASP.NET 1.1 - I have a DataGrid on an ASPX page that is databound and displays a value within a textbox. ASP.NET 1.1-我在ASPX页面上有一个DataGrid,该页面是数据绑定的,并在文本框中显示一个值。 The user is able to change this value, then click on a button where the code behind basically iterates through each DataGridItem in the grid, does a FindControl for the ID of the textbox then assigns the .Text value to a variable which is then used to update the database. 用户可以更改此值,然后单击一个按钮,该按钮后面的代码基本上会遍历网格中的每个DataGridItem,对文本框ID进行FindControl,然后将.Text值分配给变量,该变量随后用于更新数据库。 The DataGrid is rebound with the new values. DataGrid随新值反弹。

The issue I'm having is that when assigning the .Text value to the variable, the value being retrieved is the original databound value and not the newly entered user value. 我遇到的问题是,在将.Text值分配给变量时,要检索的值是原始数据绑定值,而不是新输入的用户值。 Any ideas as to what may be causing this behaviour? 关于什么可能导致这种行为的任何想法?

Code sample: 代码示例:

foreach(DataGridItem dgi in exGrid.Items)
{
    TextBox Text1 = (TextBox)dgi.FindControl("TextID");
    string exValue = Text1.Text; //This is retrieving the original bound value not the newly entered value
    // do stuff with the new value
}

So the code sample is from your button click event? 那么代码示例来自您的按钮单击事件?

Are you sure you are not rebinding your datasource on postback? 您确定不会在回发时重新绑定数据源吗?

When are you attempting to retrieve the value from the TextBox? 您何时尝试从TextBox检索值? ie when is the code sample you provided being executed? 即您提供的代码示例何时执行?

If you aren't already, you'll want to set up a handler method for the ItemCommand event of the DataGrid. 如果尚未安装,则需要为DataGrid的ItemCommand事件设置处理程序方法。 You should be looking for the new TextBox value within that method. 您应该在该方法中寻找新的TextBox值。 You should also make sure your DataGrid is not being re-databound on postback. 您还应该确保在回发时不会重新绑定DataGrid。

I would also highly recommend reading through Scott Mitchell's excellent article series on using the DataGrid control and all of it's functions: http://aspnet.4guysfromrolla.com/articles/040502-1.aspx 我也强烈建议您阅读Scott Mitchell关于使用DataGrid控件及其所有功能的出色文章系列: http : //aspnet.4guysfromrolla.com/articles/040502-1.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM