如何从该行上的文本框访问 DataGridRow?
在 DataGrid 中,当文本框中的文本更改时,我想将该行中另一个字段的值添加到数组中。
public void txtTitle_TextChanged(object sender, EventArgs e)
{
TextBox titleBox = (TextBox)sender;
DataGridItem myItem = (DataGridItem)titleBox.Parent.Parent;
string test = DataBinder.Eval(myItem.DataItem, "prod_id").ToString();
}
但是 myItem.DataItem 计算结果为 null。 我期望它评估为 DataRowView?
In a DataGrid, when text in a textbox changes I want to add the value of another field in that row to an array.
public void txtTitle_TextChanged(object sender, EventArgs e)
{
TextBox titleBox = (TextBox)sender;
DataGridItem myItem = (DataGridItem)titleBox.Parent.Parent;
string test = DataBinder.Eval(myItem.DataItem, "prod_id").ToString();
}
However myItem.DataItem evaluates as null. I was expecting it to evaluate as DataRowView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果执行以下操作,您可以触发 TextChanged 事件:
您会注意到我设置了以下属性:
自动回传=“真”
我还手动将 OnTextChanged="txtBox_TextChanged" 添加到文本框。
在我后面的代码中,我有:
事件触发的唯一方法是当您在键入后失去对文本框的焦点时。
需要考虑的要点:
这将导致回发,因此 Ajax 可能是保持良好用户体验的好方法。
您需要确保将 DataBind() 包装在 if (!IsPostBack) 中
希望这有帮助!
You can get the TextChanged event to fire if you do the following:
You will notice that i have the following properties set:
AutoPostBack="True"
I have also manually added the OnTextChanged="txtBox_TextChanged" to the text box as well.
In my code behind i have:
The only way the event will fire is when you lose focus on the text box after typing.
Key points to consider:
This will cause a post back, so Ajax might be a good way to keep the user experience nice.
You will need to make sure you wrap your DataBind() in a if (!IsPostBack)
Hope this helps!
实际上,我通过向表中添加一个自动编号列,并使用它的值来确定该行在表中的位置,然后使用它的值来影响数据网格中的相应行来解决这个问题。
我现在只是更改行的颜色,而不是将该行中的值添加到数组中,如原始问题中所述。
Effectively, I solved this by adding an autonumber column to the table, and using the value of this to determine the row's positino in the table, then using the value of this to affect the appropriate row in the datagrid.
I'm now merely changing the color of the row rather than adding values in that row to an array, as stated in the original question.