Answered by:
Trying to find a panel inside a ListView from code behind

Question
-
User426001450 posted
I do have the following panel inside a ListView like:
<ItemTemplate > <tr><th> <br /> <asp:Panel ID="RegisterMembersWaitingListControlButtonsPanel" Visible="false" runat="server"> <br /> Hello World !! <br /><br /> </asp:Panel> </th></tr> </ItemTemplate>
I'm trying to make the panel visible on page load like:
WaitingListForRegisteredArtistListView.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True
But is not working. I tried this other way :
CType(WaitingListForRegisteredArtistListView.FindControl("RegisterMembersWaitingListControlButtonsPanel"), Panel).Visible = True
But is not working either
What is it that I'm doing wrong? Thanks to all !!
Saturday, February 13, 2021 4:31 AM
Answers
-
User-1716253493 posted
First part, you can use for each or for i=0 to listview1.items.count-1
For Each item As ListViewItem In ListView1.Items item.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True Next
Second part, the sample if you want to visible it at first load only
Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles ListView1.ItemDataBound If e.Item.ItemType = ListViewItemType.DataItem Then e.Item.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True End If End Sub
So remove if not ispostback block, if you want visible it every postback
Both part 1 and 2 will show all the button in the items, usualy there is additional condition to show or hide it.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 14, 2021 2:31 PM
All replies
-
User-1545767719 posted
I'm trying to make the panel visible on page load like:
WaitingListForRegisteredArtistListView.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True
I guess that since the "WaitingListForRegisteredArtistListView" panel does not exist on the Page.Load event you cannot refer it as you mentioned. If my guess is correct, first try to find the "WaitingListForRegisteredArtistListView" panel in the handler of ListView.ItemCreated event, then apply above to the found "WaitingListForRegisteredArtistListView" panel.
Saturday, February 13, 2021 5:18 AM -
User426001450 posted
Hmm, ok.
I tried to do what you said with Item.created even but is not working. I tried Init and Load as well and it is not working either. Any other ideas or guess? Thanks
Saturday, February 13, 2021 6:00 AM -
User-1545767719 posted
I tried to do what you said with Item.created even but is not working. I tried Init and Load as well and it is not working either. Any other ideas or guess?Try below:
using System; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm18 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { Panel panel = e.Item.FindControl("RegisterMembersWaitingListControlButtonsPanel") as Panel; if (panel != null) { panel.Visible = true; } } } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm18.aspx.cs" Inherits="WebApplication1.WebForm18" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>" SelectCommand="SELECT TOP 5 [ProductID], [ProductName] FROM [Products]"> </asp:SqlDataSource> <asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnItemCreated="ListView1_ItemCreated"> <ItemTemplate> <tr style=""> <td> <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /> </td> <td> <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /> </td> <td> <asp:Panel ID="RegisterMembersWaitingListControlButtonsPanel" runat="server" Visible="False"> <br /> Hello World !! <br /><br /> </asp:Panel> </td> </tr> </ItemTemplate> <LayoutTemplate> <table runat="server"> <tr runat="server"> <td runat="server"> <table id="itemPlaceholderContainer" runat="server" border="0" style=""> <tr runat="server" style=""> <th runat="server">ProductID</th> <th runat="server">ProductName</th> <th runat="server">Panel</th> </tr> <tr id="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr runat="server"> <td runat="server" style=""></td> </tr> </table> </LayoutTemplate> </asp:ListView> </div> </form> </body> </html>
Saturday, February 13, 2021 7:01 AM -
User-1716253493 posted
To use findcontrols in the itemtemplate, you must specify item index because the listview may have multiple item
aitingListForRegisteredArtistListView.Items(0).FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True
After binding the listview, you can iterate the items
Commonly, you can set it in itemdatabound event
Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles ListView1.ItemDataBound If e.Item.ItemType = ListViewItemType.DataItem Then If Not IsPostBack Then e.Item.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True End If End If End Sub
Saturday, February 13, 2021 2:38 PM -
User426001450 posted
I thought about that but did not know how to implemented. It is working now but not at 100%. There are some minor issues that I don't know how to fix. There are some buttons and links on the page, when I click on them, sessions of the page update and the panels on the ListView disappear. I have to refresh the page to see the panels again. How could I fix this? I don't want to keep refreshing the page to see the panels. Thanks.
Sunday, February 14, 2021 2:55 AM -
User-1716253493 posted
First part, you can use for each or for i=0 to listview1.items.count-1
For Each item As ListViewItem In ListView1.Items item.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True Next
Second part, the sample if you want to visible it at first load only
Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles ListView1.ItemDataBound If e.Item.ItemType = ListViewItemType.DataItem Then e.Item.FindControl("RegisterMembersWaitingListControlButtonsPanel").Visible = True End If End Sub
So remove if not ispostback block, if you want visible it every postback
Both part 1 and 2 will show all the button in the items, usualy there is additional condition to show or hide it.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 14, 2021 2:31 PM -
User426001450 posted
Ok, I just remove the IsPostBack and is always showing the panels now. This is the way that I want it to be. Thanks a lot for your help.
Sunday, February 14, 2021 4:34 PM