Hi,
I Want to Hide delete and edit button from user, but still it should available to admin… only the problem is it is in commandfield…. my code is like below:
<asp:GridView
ID="GridView1"
DataKeyNames="ID"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
OnRowDeleting="GridView1_RowDeleting"
OnRowCommand="GridView1_RowCommand"
AllowPaging="True"
AllowSorting="True"
PageSize="3"
>
<Columns>
<asp:BoundField
DataField="ID"
ReadOnly="True"
HeaderText="ID"
SortExpression="ID"
/>
<asp:BoundField
DataField="TITLE"
HeaderText="TITLE"
SortExpression="TITLE"
/>
<asp:ButtonField
ButtonType="Button"Text="Download" CommandName="btndownload"
/>
<asp:CommandField
ButtonType="Button"
ShowDeleteButton="True"
ShowEditButton="True"
/>
</Columns>
</asp:GridView>
I know i can use something like
Visible= ‘
<%#(bool) ShowDeleteRowBasedOnRole()%>‘ and then keep that function in code behind file like:
protected bool ShowDeleteRowBasedOnRole()
{
if (Roles.IsUserInRole("Administrators")
|| Roles.IsUserInRole("HillEditors"))
{
return true;
}
else
{
return false;
}
}
(the above is not my code i found on one of asp.net forum.)
when i run the program it is giving me error like:
Error 1 Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.ButtonField does not have a DataBinding event…..
so i m really stuck here….. can anyone help me in this??????
You are actually showing my code! I think if you convert the command field into a template it will work.
Here is some of the real code with the template
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
Text="Select">
</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" Visible=’<%# (bool) ShowDeleteRowBasedOnRole() %>’>
</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
You could also set the visibility programmatically like:
if (!User.IsInRole("Admin"))
GridView1.Columns[3].Visible = false;
Hi,BhaveshPatel:
I suggest you to do that
protected void ShowDeleteRowBasedOnRole()
{
if (Roles.IsUserInRole("Administrators")
|| Roles.IsUserInRole("HillEditors"))
{
this.GridView1.AutoGenerateDeleteButton=true;
this.GridView1.AutoGenerateEditButton =true; }
else
{
this.GridView1.AutoGenerateDeleteButton=false; this.GridView1.AutoGenerateEditButton =false; }
}
Very nice! I should have thought of that. Much cleaner than using the template.
Hi, Thanks to pkellner, rexlin and agolden.
Just to hide the column works well with my code… as per agolden.
As per pkellner: sorry to say you sir, but I am not using linkbutton. it is bound field and bound field gives compilation error with Visible=’<%# (bool) ShowDeleteRowBasedOnRole() %>’ .. so i dont think that cod works for me.
As Per Rexlin: as per you it is creating a new column at very first column of gridview….. and i want it at the last column……
Once again thanks a lot for your time and help.
Gang:
- How do you pass boolean visibility value to linkbutton in ASP.net 2005 (VB)?
- Not sure about visible code?
<asp:LinkButton
ID="lbtnDeleteOrder"
Runat="server"
OnClientClick="return confirm(‘Are you sure you want to permanently delete this record?’);"
CommandName="Delete"
ForeColor =
"red" Visible=’<% #test() %>‘>Delete</asp:LinkButton>
Here is script:
<script
runat="server">
Protected Sub test(ByVal visible
as Boolean)
If Session("access") =
"LEVEL1" Then
visible = True
End If
End Sub
</script>
Thank you in advance for prompt professional reply.
hai BhaveshPatel
am trying to delete a row using command field its working fine but i wanna add delete confirmation I saw some sites it is suggesting itemtemplate or linkbutton for confirmation will you please help me
hai BhaveshPatel
am trying to delete a row using command field its working fine but i wanna add delete confirmation I saw some sites it is suggesting itemtemplate or linkbutton for confirmation will you please help me
Jhanani(inbajh_1984@yahoo.co.in)
Hi Jhanani!
may be you can think of using Javascript for that. like below
<script>
function confirmDelete(delUrl)
{
if (confirm("Are you sure you want to delete")) {
document.location = delUrl;
}
}
</script>
<a
href="javascript:confirmDelete(‘delete.page?id=1′)">Delete</a>
or you can directly add the below code like this:
<a
href="delete.page?id=1"
onclick="return confirm(‘Are you sure you want to delete?’)">Delete</a>
cheers…
hi bhavesh,
thanks .i already added delete button using commandfield.is there any way to add confirmation for deletion when i am showing delete button by setting AutoGenerateDeletebutton to true or by commandfield .i dont wanto use templatefield for delete.Please
help
hi
I am having 3 controls dropdownlist,gridview and detailview.If i click any category bound in dropdown that will affect in gridview and shows products in that category.I set AutoGenerateSelectButton to true and when i click select the paricular product’s
detail is shown in detailview.In Detail view am having editbutton.if edit its working well.But in grid it shows old value.
example:Category:Soap
Gridview Detailview
ProductName Price Productname: Pears
select Pears 20 Price: 30(after edit)
Edit
So how to refresh gridview
If it is autogenerated delete button, then it may be hard to find that button from your aspx page and then add some confirmation to that delete button.
If rather than creating a autogenerate delete column if you can put link button or some button then simply you can add this code
OnClientClick="return confirm(‘Are you certain that you want to delete this Record?’);" and it should work welll….
But still if you wanna keep autogenerate delete button, then you can think of adding javascript on page_Load event by using Button1.Attribute.Ad()….. but for that also you atleast have to find out the autogenrated delete
column in page load event.
This should work for your refreshing gridview… use the below code:
protected void Page_PreRenderComplete(object sender,
EventArgs e)
{
GridView1.DataBind();
// To refresh the gridview when delete,update or insert occurs.
DetailView1.DataBind();
}
thanks bhavesh
am trying to bind sitemap from database.i set the sitemap provider and connection string in web.config.am using sitemappath control and i set its provider name.but i dont know how to retrieve and bind to the control.
In database am heaving ID,title,URL,parentId i considered root node having null values in parentid.please help
thanks bhavesh
am trying to bind sitemap from database.i set the sitemap provider and connection string in web.config.am using sitemappath control and i set its provider name.but i dont know how to retrieve and bind to the control.
In database am having ID,title,URL,parentId i considered root node having null values in parentid.please help
hi pkellner
I wanto hide address bar ,toolbar of the current window while page is loading for example i have IDCard link in the left control.
If i click that link redirect to PrintableIDPage.aspx .But i dont want any toolbar,addressbar in that page.I saw so many examples but those showing
hiding tollbar of popup widow /Please help
janani
Hey guys!!!
I will say better you post this kind of questions in new thread rather than replying in "Want to Hide delete and edit button from user, but still available to admin" thread….
If you will use the new thread in new question, then definately you will get answer from some asp.net team guy…. in this replying thing only I am getting your questions(i am not a asp.net team guy), no one else…. so better start new thread and u will
have quick reply..
enjoy with ASP.NET
Thanks to ASP.NET Team.
Hi rexlin,
I would just like to thank you for the code you posted. It was really helpful.
Thanks once again.
When I use this code, I get the message – Cannot convert type ‘void’ to ‘bool’.
rexlin
Hi,BhaveshPatel:
I suggest you to do that
protected void ShowDeleteRowBasedOnRole()
{
if (Roles.IsUserInRole("Administrators")
|| Roles.IsUserInRole("HillEditors"))
{
this.GridView1.AutoGenerateDeleteButton=true;
this.GridView1.AutoGenerateEditButton =true; }
else
{
this.GridView1.AutoGenerateDeleteButton=false; this.GridView1.AutoGenerateEditButton =false; }
}
When I use this code, I get the message – Cannot convert type ‘void’ to ‘bool’. I am using Visible=’<%# (bool) ShowDeleteRowBasedOnRole() %>’ from the template for the button.
Disregard this message. I can’t believe it doesn’t give me a delete option! I hate your posts, I can’t delete my message and it justs adds more messages. How stupid a disign this is!