Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 17th, 2009, 10:37 AM
Authorized User
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default Listview: How to make the Insert Button invisible through code behind


I have a Listview, where some users (depending on their role) are allowed to edit, delete and insert columns and others are only allowed to read the data.
Hence I have set the Insert, Delete and Edit buttons as invisible by default. In the code behind I tried to make the buttons visible for the user with the right role. This works fine for the Edit button and the Delete button, where I use the GridView_RowDataBound code from Chapter 14 page 495, with some adaptation because my ListView shows the columns in a single line.
For the Insert button however I can't find an Event that targets the Insert button. I tried ItemCommand, ItemCreated, ItemDataBound, DataBinding, Load for the ListView and pageLoad as well. Is there a way to target the Insert button?
Even more attractive would be to make the whole Insert template visible/invisible by a single command, but I guess that's impossible.

My code is
<asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" 
        Text="Insert" ValidationGroup="Insert" Visible="False" />

//Code behind

protected void ListViewSchalen_ItemDataBound(object sender, ListViewItemEventArgs e)
    if (Roles.IsUserInRole("Neurologie"))
      switch (e.Item.ItemType)
        case ListViewItemType.DataItem:
          LinkButton myDeletebtn = (LinkButton)e.Item.FindControl("DeleteButton");
          if (myDeletebtn != null)
            myDeletebtn.Visible = true;
          LinkButton myEditbtn = (LinkButton)e.Item.FindControl("EditButton");
          if (myEditbtn != null)
            myEditbtn.Visible = true;
          LinkButton myInsertbtn = (LinkButton)e.Item.FindControl("InsertButton");
          if (myInsertbtn != null)
            myInsertbtn.Visible = true;
          LinkButton myCancelbtn = (LinkButton)e.Item.FindControl("CancelButton");
          if (myCancelbtn != null)
            myCancelbtn.Visible = true;

  #2 (permalink)  
Old May 17th, 2009, 12:23 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts

I'm not sure what book you're following, but the usual approach to something like this would be to place all of your editing/deleting/inserting controls in a single enclosing control such as a Panel, then just make the Panel visible if the user is in the correct role.
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

if (this.PostHelpedYou)
  #3 (permalink)  
Old May 17th, 2009, 03:42 PM
Authorized User
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts

Thanks Lee,

The book is "Beginning ASP.Net 3.5 in C# and VB".
Using a Panel will make things a lot easier. That would allow me to make the whole <InsertItemTemplate> visible/invisible.
But which Event do I need to use when I make the Panel visible?

Best regards,

  #4 (permalink)  
Old May 24th, 2009, 12:41 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

Hi Hans,

While the Panel approach works great for hiding many controls at the same time, the ListView makes this even easier by supplying a None value for the InsertItemPosition. When set to none, the entire InsertItem is hidden:
if (!User.IsInRole("SomeRole"))
  ListView1.InsertItemPosition = InsertItemPosition.None;
Hope this helps,

Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following 3 Users Say Thank You to Imar For This Useful Post:
fawel (July 18th, 2016), hans (June 1st, 2009), jimjamjo (May 13th, 2010)
  #5 (permalink)  
Old July 18th, 2016, 03:04 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jul 2016
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Hide whole insertItemTemplate in a ListView

Imar's code is great! I did it following his code.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make the print button invisible dipti.agrawal ASP.NET 2.0 Basics 0 March 11th, 2007 01:20 PM
Make a image button as default submit button toshi ASP.NET 1.0 and 1.1 Basics 1 June 1st, 2006 05:25 AM
Make visible/invisible stealthdevil Access VBA 6 May 31st, 2006 10:59 AM
cannot insert OLE command button event code janefu Excel VBA 1 January 10th, 2006 04:33 AM

All times are GMT -4. The time now is 06:24 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.