简体   繁体   English

如何从代码隐藏访问位于列表视图中的控件?

[英]How to access controls located in the listview from codebehind?

I have a ListView called CouncilListView and a TextBox called EmailTextBox which is in the ListView. 我有一个名为的ListView CouncilListView和一个名为文本框EmailTextBox这是在ListView。

How can I access this TextBox from the CodeBehind? 如何从CodeBehind访问此TextBox?

I tried some forms of FindControl like : 我尝试了某些形式的FindControl例如:

this.Page.FindControl("EmailTextBox");
this.Page.FindControl("CouncilListView").FindControl("EmailTextBox");
this.CouncilListView.Findcontrol("EmailTextBox");
this.FindControl("EmailTextBox");

but I get this error: 但是我得到这个错误:

Object reference not set to an instance of an object.

This is hypothetical, since I can't see your complete page codebehind and ListView code, but: 这是假设的,因为我看不到您的完整页面代码和ListView代码,但是:

The reason is that the Textbox is part of the Listview; 原因是文本框是Listview的一部分。 so you need to find it in the ListView. 因此您需要在ListView中找到它。 One possible way of doing this is below. 下面是一种可能的方法。

public void GetTextBoxValuesFromListView()
{
    Textbox tb = null;
    ListViewItem Item = null;
    foreach (ListViewDataItem item in CouncilListView.Items)
    {
        Item = item;
        tb = ((TextBox) (Item.FindControl("EmailTextBox")));
        if (tb.Text != null)
        {
            //Do something
        }
    }
}

I had some issues with ListViews I had questions on some time back, they may be of use to you: 我在使用ListViews时遇到了一些问题,不久前又遇到了问题,它们可能对您有用:

I solved it this way: 我是这样解决的:

protected void CouncilListView_ItemInserted(Object sender, ListViewInsertedEventArgs e)
{
    foreach (DictionaryEntry Emailentry in e.Values)
    {
        if (Emailentry.Key == "Email") //table field name is "email"
        {
            message.To.Add(new MailAddress(Emailentry.Value.ToString()));
        }
    }
}

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

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