Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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

Hi,

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
Code:
<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;
          }
          break;
      }
    }
  }
thanks

Hans
 
Old May 17th, 2009, 12:23 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old May 17th, 2009, 03:42 PM
Authorized User
 
Join Date: Feb 2009
Posts: 10
Thanks: 1
Thanked 2 Times in 2 Posts
Default

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,

Hans
 
Old May 24th, 2009, 12:41 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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:
Code:
 
 
if (!User.IsInRole("SomeRole"))
{
  ListView1.InsertItemPosition = InsertItemPosition.None;
}
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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)
 
Old July 18th, 2016, 03:04 AM
Registered User
 
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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.