Solved

FindControl in ListView inside ItemTemplate

Posted on 2009-07-07
10
7,150 Views
Last Modified: 2013-12-17
Hi Experts,

I want to find a Button "btnDelete" inside an ItemTemplate of a ListView to control his visibility and couldn't find a way.
Below the aspx and the C# code I tried.

Hope you can bail me out.

7oop4q

PS: Framework 3.5 SP1
aspx:
 
<asp:ListView runat="server" DataKeyNames="pid_sys_Files" ItemPlaceholderID="itemPlaceholder" ID="lvFileList" OnInit="lvFileList_OnInit">
  <LayoutTemplate>
    <table class="gridview" cellpadding="0" cellspacing="0" width="100%">
      <tr class="header">
        <th><asp:Localize ID="Filename" runat="server" meta:resourcekey="Filename"></asp:Localize></th>
        <th><asp:Localize ID="Filesize" runat="server" meta:resourcekey="Filesize"></asp:Localize></th>
        <th><asp:Localize ID="Date" runat="server" meta:resourcekey="Date"></asp:Localize></th>
        <th>&nbsp;</th>
      </tr>
      <tr id="itemPlaceholder" runat="server"></tr>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr class='<%# Container.DataItemIndex % 2 == 0 ? "row" : "row alt" %>'>
      <td><a href='<%# Eval("pid_sys_Files", "addActions.aspx?document={0}") %>' runat="server"><%# Eval("OriginalName") ?? "&nbsp;"%></a></td>
      <td><%# FormatFS(Eval("FileSize"))%></td>
      <td><%# Eval("DateCreated") ?? "&nbsp;"%></td>
      <td><asp:Button ID="btnDelete" CommandArgument='<%# Eval("pid_sys_Files") %>' OnClick="btnDelete_OnClick" runat="server" meta:resourcekey="DeleteFile" /></td>
    </tr>
  </ItemTemplate>
</asp:ListView>
 
 
C#:
 
// the new instance is always null and I can't find the button inside the Controls array
Control c = lvFileList.Controls[0].FindControl("btnDelete");
 
// this one also
Button BtDelete = ((Button)lvFileList.FindControl("ctl00_ContentPlaceHolder1_TabContainer1_TabFiles_lvFileList_ctrl0_btnDelete"));
BtDelete.Visible = false;
 
// and this one too
(Button)lvFileList.FindControl("btnDelete").Visible = false;

Open in new window

0
Comment
Question by:7oop4q
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24792019
Did this one worked or not
// this one also
Button BtDelete = (Button)lvFileList.FindControl("btnDelete");
BtDelete.Visible = false;


// and this one too
((Button)lvFileList.FindControl("btnDelete")).Visible = false;
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 24792020
ucan access these controls while databinding event..

and u cave to check the typeof the item.. u can not access those controls in item template,in header or footer item..

0
 
LVL 39

Expert Comment

by:abel
ID: 24792202
It will not work to try to find the button using FindControl on the full filelist, because the list will contain many btnDelete controls, one for each list item. To find your control you have basically three options:

1. Use the rowdatabinding event (not sure if ListView has that)
2. Assign an event to the button OnLoad event
3. Loop through all listview items. On each item you can do a FindControl for the controls in that ListViewDataItem

-- Abel --
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24792226
Point one above should be: OnItemDataBound event. It gives you the current item while it's being databound. You can use FindControl on the e.Item of the ListViewItemEventArgs:

protected void lvFileList_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
   // get the button or any other control on the current row
   Button deleteButton = (Button) e.Item.FindControl("btnDelete");
   if(deleteButton != null)
   {
       // button can be null for non-dataitem rows
       // do your thing with the button.
   }
}

Open in new window

0
 
LVL 1

Author Comment

by:7oop4q
ID: 24792246
@ragi0017: no both of them hasn't

@non_zero: i tried to acces them on th databound event but I don't know how to access the sender.
You mean i don't have a chance to access the controls inside a template?

@abel: 1. ListView does'n have the RowDataBinding event like GridView has
2. and then what?
3. I will try this. Sounds interesting.

0
 
LVL 39

Expert Comment

by:abel
ID: 24792273
> @abel: 1. ListView does'n have the RowDataBinding event like GridView has
Yes, I checked and saw that too, I also get sometimes confuses with the different terminology Microsoft uses everywhere. See my last comment for what I meant.

> 2. and then what?
Inside the event handler you can cast the sender to a type Button. You can use the Parent (or Parent.Parent) to find the containing control if you need to find other controls, which you can do from there with FindControl again. But point 1 and point 3 are perhaps more straightforward depending on your situation.
0
 
LVL 9

Expert Comment

by:Rahul Goel ITIL
ID: 24792278
In RowDataBinding  event

((Button)e.Controls.FindControl("btnButton")).Visible = false;
Toggle it
0
 
LVL 39

Expert Comment

by:abel
ID: 24792287
@Rahu: see the last three comments where we emphasized that there is no RowDataBinding event.
0
 
LVL 1

Author Closing Comment

by:7oop4q
ID: 31600494
Ok that was it! Plain, simple and ready to use :)

Thanks!
0
 
LVL 39

Expert Comment

by:abel
ID: 24792388
Thanks for the follow-up and the grade ;-)
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question