user1
user1

Reputation: 780

Unable to FindControl() in ListView ItemEditing

I have a ListView in an ASP.NET web application. When a user clicks the edit button, I want textfields to pop up that are dependent on certain values of the item. However, I can't seem to find any controls inside of my ListView1_ItemEditing() function.

I have read the Microsoft documentation and various help threads on the internet, but their suggestions do not appear to work for me. This is generally what I see:

ListViewItem item = ProductsListView.Items[e.NewEditIndex];
Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");

For the sake of simplicity I just want to be able to select a label in ListView1_ItemEditing(). This is the code in ListView1_ItemEditing():

protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
    DataBind(); //not sure if this does anything
    ListViewItem item = ListView1.Items[e.NewEditIndex];
    Label debugLabel = (Label)item.FindControl("label_editing");
    debugLabel.Text = "Works";
}

Here is the ASP

<EditItemTemplate>
   <asp:Label ID="label_editing" runat="server" Text="hello world"></asp:Label>
</EditItemTemplate>

When debugging, item and debugLabel are both NULL.

UPDATE: I resolved this issue by moving my logic to ItemDataBound and then checking if my tr (containing textboxes) was in that particular data item. Code below:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Control tr_verizon = e.Item.FindControl("tr_verizonEdit");
            Control tr_att = e.Item.FindControl("tr_attEdit");
            if (tr_verizon != null)
            {
                //Control tb_meid = e.Item.FindControl("TextBox_Meid");
                Label lbl_carrierId = (Label)e.Item.FindControl("lbl_carrierId");
                if (lbl_carrierId == null)
                {
                    Message.Text = "lbl_carrierId is null!";
                }
                else if (lbl_carrierId.Text.Equals(""))
                {
                    Message.Text = "lbl_carrierId is empty!";
                }
                else
                {
                    string recordId = lbl_carrierId.Text;

                    if (tr_verizon != null && tr_att != null)
                    {
                        if (lbl_carrierId.Text.Equals("1"))
                        {
                            tr_verizon.Visible = false;
                            tr_att.Visible = true;
                        }
                        else
                        {
                            tr_verizon.Visible = true;
                            tr_att.Visible = false;
                        }
                    }
                }
            }
        }
    }

Upvotes: 1

Views: 7150

Answers (3)

Sven
Sven

Reputation: 21

You should do the DataBind() first, like this:

    ListView1.EditIndex = e.NewEditIndex;
    ListView1_BindData(); // a function that get the DataSource and then ListView1.DataBind()

// Now find the control as you did before

Upvotes: 2

Kaf
Kaf

Reputation: 33809

The ItemEditing event is raised when an item's Edit button is clicked, but before the ListView item is put in edit mode. Therefore controls in EditItemTemplate are not available at this time.

More Info and example

Upvotes: 2

SouthShoreAK
SouthShoreAK

Reputation: 4296

Have you tried casting the sender object instead of trying to access your ListViewItem by index?

protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
    var item = sender as ListViewItem;
    var debugLabel = item.FindControl("label_editing") as Label;
    debugLabel.Text = "Works";
}

Upvotes: 0

Related Questions