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.