How to disable a GridView delete button depending on a value in the datasource?

K

keithb

I have a GridView control that has a delete button (hyperlink) in one
column. The datasource table contains a column that has a true/false
indicator showing whether or not the record can be deleted. How can I
enable/disable the delete button depending on the value of the indicator?
Ideally, I would like to make the button invisible where a record cannot be
deleted.

Thanks,

Keith
 
C

Craig Deelsnyder

keithb said:
I have a GridView control that has a delete button (hyperlink) in one
column. The datasource table contains a column that has a true/false
indicator showing whether or not the record can be deleted. How can I
enable/disable the delete button depending on the value of the indicator?
Ideally, I would like to make the button invisible where a record cannot be
deleted.

Thanks,

Keith

You will pry have to create your own TemplateColumn that will hold a
delete button (add it to the template(s)). Set its CommandName="Delete"
and it will replace the one 'built into' the GridView just fine (also
disable Deleting in the GridView's properties, otherwise the default one
will also show up).

Once you have a button, you can bind its Visible property to the value
of the indicator.
 
Joined
Jun 20, 2006
Messages
1
Reaction score
0
Implementing Craig's advice

Craig Deelsnyder said:
keithb wrote:
> I have a GridView control that has a delete button (hyperlink) in one
> column. The datasource table contains a column that has a true/false
> indicator showing whether or not the record can be deleted. How can I
> enable/disable the delete button depending on the value of the indicator?
> Ideally, I would like to make the button invisible where a record cannot be
> deleted.
>
> Thanks,
>
> Keith
>
>


You will pry have to create your own TemplateColumn that will hold a
delete button (add it to the template(s)). Set its CommandName="Delete"
and it will replace the one 'built into' the GridView just fine (also
disable Deleting in the GridView's properties, otherwise the default one
will also show up).

Once you have a button, you can bind its Visible property to the value
of the indicator.

--
Craig
Microsoft MVP - ASP/ASP.NET

Hi there,

Hope the following reply is still relevant. I did this in an ASP.NET 2.0 page.

I took on Craig's advice and did the followin things:
1. I exposed a property in my datasource that returns a boolean indicating whether or not a "Delete" should be visible.
e.g.

public bool allowDelete
{
get { return (this.id < 0); }
}


2. I then added the following in my grid:
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="GridSelectButton" runat="server" Text='<%# Eval("FullName")%>' CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="GridDeleteButton" runat="server" Text="Delete" CommandName="Delete" Visible='<%# Eval("AllowDelete")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


As can be seen, I've also added another "Select" option, whose text evaluates the "FullName" field of the datasource.

3. I then added an event handlers in the .cs code
#region GridView1_SelectedIndexChanging
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
// Code that works with the selected row
}
#endregion

#region GridView1_RowDeleting
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Remove the row from the data source
localDataSource.RemoveAt(e.RowIndex);
GridView1.DataSource = localDataSource;
GridView1.DataBind();
}
#endregion


That works for me. Hope it'll work for you, too.

Ta.
 
Joined
Dec 12, 2008
Messages
1
Reaction score
0
Finally got it

The previous message was incomplete and/or was using methods that I am unfamiliar with. Here's how I finally got it working. Use the LinkButton as before:

Code:
        <asp:LinkButton ID="IB3" runat="server" Visible='<%# Eval("AllowDelete")%>' Text="delete" CommandName="delete" OnClientClick="return confirm('Are you sure you want to delete this allergy?');" />

in the datasource, declare a control parameter like this:

Code:
<SelectParameters>
                <asp:ControlParameter ControlID="theAllowDelete" Name="AllowDelete" PropertyName="Text" Type="Boolean" />

It is assumed that your select statment includes something like this: select @AllowDelete as AllowDelete,...from tablename where ...

(Notice that the control parameter is boolean.) Towards the end of your form tag, include a hidden field like this:

Code:
<asp:TextBox ID="theAllowDelete" runat="server" Visible="False"></asp:TextBox><br />

Finally, somewhere in your code you need to set theAllowDelete as here:

Code:
theAllowDelete.Text="False";
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top