views:

300

answers:

2

I have a ListView called CouncilListView and a TextBox called EmailTextBox which is in the ListView.

How can I access this TextBox from the CodeBehind?

I tried some forms of FindControl like :

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.

+1  A: 

This is hypothetical, since I can't see your complete page codebehind and ListView code, but:

The reason is that the Textbox is part of the Listview; so you need to find it in the 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:

George Stocker
A: 

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()));
        }
    }
}
Mahdi
So you didn't provide any actual code that made up your listview, and then commented that the provided code didnt' work? You've got to give us more than that; if we can't see your code, then we're just firing bullets in the dark.
George Stocker