Selectively hiding Button in DataList ItemTemplate

baisr lesbienne

New Member
How can I hide a button that's part of a DataList ItemTemplate? I want to have an 'Edit' button that is only available for administrators. So, when the page loads I check to see if the currently logged in user is an Administrator or not. Based on that I want to show/hide the 'Edit' button.<BR>It should be simple, I just don't know how to drill down into a control located in the ItemTemplate.<BR><BR>I have the same question for EditItemTemplate.<BR><BR>Thanks,<BR>GregHi Greg, <BR><BR>One way to do this would be to first identify if the user is an admin. If they are not, loop through the items in your datagrid and use FindControl to set the visibility of your buttons to false like so:<BR><BR>dim i AS integer<BR>dim editButton AS Button<BR><BR>For i=0 To yourGridName.Items.Count - 1<BR> editButton = yourGridName.Items(i).FindControl("nameOfYourButton").visible = false<BR>Next<BR><BR>Another way would be to put the edit button in it's own column and then just hide or show the entire column if the user is an admin. <BR><BR>Hth,<BR><BR>])rySweet. Here's what I ending up doing. I made a Sub that I call on Page_Load:<BR><BR>Private Sub ShowHideEditBtn<BR> Dim i As Integer<BR><BR> ' Hide the 'Edit' button unless they are an 'Admin'<BR> If Session("UserRole").ToString <> "Admin" Then<BR> For i = 0 To datEmps.Items.Count - 1<BR> datEmps.Items(i).FindControl("btnEdit").Visible = False<BR> Next<BR> End If<BR><BR> End Sub<BR><BR><BR>How would I hide an entire column? I don't see a Columns collection for a datagrid.<BR><BR>Thanks,<BR>GregHey Greg, <BR><BR>To hide/show an entire column in a datagrid you just set the column visible property to true or false. Something like:<BR><BR>yourDataGrid.Columns(colIndex).visible = true/false<BR><BR>And there is a columns property for the dataGrid, found here:<BR><BR>ms-help://MS.NETFrameworkSDK/cpref/html/frlrfSystemWebUIWebControlsDataGridClassColumnsTop ic.htm<BR><BR>Hth,<BR><BR>])ry
 
Back
Top