Reference Datagrid EditCommandColumn In Code

S

Scott McDaniel

I'm using Visual Studio 2005 with the .NET 2.0.50727 framework, and we're using the standard Datagrid control bound to a
SQL Server.

We have two groups of users - "full edit" users, and "view only" users. We would like to disable the EditCommandColumn
of our Datagrid (named dgCertificates) for viewonly users. The intent is that viewonly would, obviously, only be able to
view data and not maniupulate it in any way.

I've tried the FindControl methods but I can't seem to figure out the ID or Name of this column ... I've tried running
through the columns, but I can't seem to grad the column with this either.

Is there a way to do this? Can I simply "disable" the grid while still allowing viewonly users to interact with the grid
(i.e. page through, etc) without allowing any data edits/updates/deletems/adds?

Thanks
 
S

Steven Cheng[MSFT]

Hi Scott,

Welcome to the MSDN newsgroup.

From your description, I understand that you're developing an ASP.NET 2.0
web application. In one page, you're using DataGrid control(or GridView?)
and the grid has one edit command column. However, you're wondering how to
make the edit/update function only available to certain grant users,
correct?

Based on my experience, we can do this through serveral means:

1. We can get the certain EditCommand column from the GridView or DataGrid
control's "Columns" collection (through index) and then set its visble to
true/false according to the current visit user's identity.

2. If you do not want to hide the entire column, we can consider use the
DataGrid's ItemDataBound or GridView's RowDataBound event, in these events,
we can get certain inner control's reference from the column/cell and then
set their enabled property. BTW, it is recommend that we use template
column/template field for such scenario so that it'll be easier to
determine the control's ID so as to use FindControl to locate them.

Also, if convenient, you can post the detailed page template and code
snippet here so that we can help have a look to see whether there is
anything incorrect.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

SK

I have situations that require both methods . On some pages, I would like
to control whether the user sees the Edit/Update or Delete button at the
column level. On others, some users only have Edit on certain rows that fit
my criteria for 'editable row'.

In my current ASP pages, I have a vb function that determines the User's
permission level for that function on that page and then displays the HTML
if they have the permission.

I am very new to ASP.NET 2.0, so have no idea how to go about implementing
this. Do you have any examples you could point me to?

I wish role-based security was able to be implemented at this more granular
level (rather than page).

Thanks,

Sue
 
S

Scott McDaniel

From your description, I understand that you're developing an ASP.NET 2.0
web application. In one page, you're using DataGrid control(or GridView?)
and the grid has one edit command column. However, you're wondering how to
make the edit/update function only available to certain grant users,
correct?

Yes, that is correct. I'd prefer to hide the column; currrently I'm disabling the entire grid like this:

dgAircraft.Enabled = Session("user_type") <> "VIEWONLY"

Two other items which will certainly have an impact: I'm using MasterPages, and the Datagrids which need to be disabled
are on Web User Controls.

1. We can get the certain EditCommand column from the GridView or DataGrid
control's "Columns" collection (through index) and then set its visble to
true/false according to the current visit user's identity.

2. If you do not want to hide the entire column, we can consider use the
DataGrid's ItemDataBound or GridView's RowDataBound event, in these events,
we can get certain inner control's reference from the column/cell and then
set their enabled property. BTW, it is recommend that we use template
column/template field for such scenario so that it'll be easier to
determine the control's ID so as to use FindControl to locate them.

Also, if convenient, you can post the detailed page template and code
snippet here so that we can help have a look to see whether there is
anything incorrect.

Here's a sample from one of the UserControls ascx pages. Unfortunately I deleted the code where I was attempting to
locate the column, so I have nothing to post regarding that. I was trying to run the code in the Page_LoadComplete
event.


<asp:DataGrid id=<%@ Reference Control="~/WebControls/commoncontrol.ascx" %>
<%@ Register TagPrefix="igsch" Namespace="Infragistics.WebUI.WebSchedule" Assembly="Infragistics.WebUI.WebDateChooser.v5.3, Version=5.3.20053.50, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" %>
<%@ Control Language="vb" AutoEventWireup="false" Inherits="webPilotLogBook.Certificates" CodeFile="Certificates.ascx.vb" CodeFileBaseClass="webPilotLogBook.CommonControl" %>

<asp:DataGrid id="dgCertificates" runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="3" GridLines="Vertical" Font-Size="8pt" Font-Names="Verdana"
AutoGenerateColumns="False" ShowFooter="True" AllowPaging="True" Width="600px">
<FooterStyle ForeColor="Black" BackColor="#CCCCCC" />
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C" />
<AlternatingItemStyle BackColor="Gainsboro" />
<ItemStyle ForeColor="Black" BackColor="#EEEEEE" />
<HeaderStyle CssClass="flightLogField" />
<Columns>
<asp:BoundColumn Visible="False" DataField="lngCertificateID" SortExpression="lngCertificateID"
HeaderText="lngCertificateID"></asp:BoundColumn>
<asp:BoundColumn DataField="strGrade" SortExpression="strGrade" HeaderText="Grade"></asp:BoundColumn>
<asp:BoundColumn DataField="strNumber" SortExpression="strNumber" HeaderText="Number"></asp:BoundColumn>
<asp:BoundColumn DataField="dteDateIssued" SortExpression="dteDateIssued" HeaderText="Date Issued"
DataFormatString="{0:d}">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="strRemarks" SortExpression="strRemarks" HeaderText="Remarks"></asp:BoundColumn>
<asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="DELETE">
<ItemStyle HorizontalAlign="Right"></ItemStyle>
<FooterStyle HorizontalAlign="Right"></FooterStyle>
</asp:ButtonColumn>
</Columns>
 
S

Steven Cheng[MSFT]

Thanks for the response Scott,

How about using the below code to hide the certain column in datagrid?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Context.User.Identity.Name = "steven" Then
dgCertificates.Columns(4).Visible = True
dgCertificates.Columns(5).Visible = True

End If

End Sub

In your case, the Edit and Delete butotn columns are the 5th and 6th column
in the datagrid.

Hope this helps.


Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Scott McDaniel

On Fri, 31 Mar 2006 11:01:17 GMT, (e-mail address removed) (Steven Cheng[MSFT]) wrote:

Thanks, I'll try this out and see ...
Thanks for the response Scott,

How about using the below code to hide the certain column in datagrid?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Context.User.Identity.Name = "steven" Then
dgCertificates.Columns(4).Visible = True
dgCertificates.Columns(5).Visible = True

End If

End Sub

In your case, the Edit and Delete butotn columns are the 5th and 6th column
in the datagrid.

Hope this helps.


Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
S

Steven Cheng[MSFT]

Thanks for your response Scott,

Please feel free to let me know if there is anything else we can help.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top