ListView查找项目并填充texbox [英] ListView find item and populate texbox
问题描述
大家好,我有一个ListView,其中包含客户姓名,地址和电子邮件地址。我需要找到电子邮件地址,然后填充一个文本框。
以下代码但没有工作错误消息;
mscorlib.dll中出现'System.ArgumentOutOfRangeException'类型的异常,但未在用户代码中处理
附加信息:索引超出范围。必须是非负数且小于集合的大小
我的代码到目前为止...
Hi folks, I have a ListView that contains customer name, address and email address. I need to find the email address and then populate a textbox.
Code below but not working error message;
An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code
Additional information: Index was out of range. Must be non-negative and less than the size of the collection
My code so far...
int idx = ListView1.SelectedIndex;
TextBox GarageEmail.Text = (TextBox)this.ListView1.Items[idx].FindControl("SupplierEmailAddress");
推荐答案
SelectedIndex是零 - ListView控件中所选项的基于索引。默认值为-1,表示当前没有选择任何项。 (来自 MSDN [ ^ ])...所以你应该检查它是否不是-1,以确保任何项目被选中......
SelectedIndex is "The zero-based index of the selected item in a ListView control. The default is -1, which indicates that no item is currently selected." (from MSDN[^])...So you should check if it is NOT -1 to be sure that any item has been selected...
if(ListView1.SelectedIndex != -1)
{
int idx = ListView1.SelectedIndex;
TextBox GarageEmail.Text = (TextBox)this.ListView1.Items[idx].FindControl("SupplierEmailAddress");
}
现在换第二行......这是完全错误的?左侧部分,如果TextBox的文本属性(不清楚它来自哪里),右侧是TextBox控件...你可能想做这样的事情:
Now for the second line...It is completely wrong? The left side part if the text property of a TextBox (not clear from where it comes), where the right side is a TextBox control...You probably meant to do something like this:
TextBox GarageEmail = (TextBox)this.ListView1.Items[idx].FindControl("SupplierEmailAddress");
这篇关于ListView查找项目并填充texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!