datagrid templates and deletion

D

Dan

Hi all, I have a question about datagrid: let's say I have a datagrid which
shows the categories for some items; each category has an ID field and a
description. I use an ASP.NET page with a datagrid bound to this table to
show, edit or delete any row in this table. I have made a template of the
edit/cancel column to be able to check for invalid values (e.g. an empty or
duplicate category), using validators referring to the edit control of my
edit template.

All this works fine, my question is: I'd like to Disable the delete command
for all the rows which are assigned to some items in the database, so that
it will not be possible to delete a category without first modifying all the
items assigned to it. Anyway, the user will be able to delete unused
categories, either existing or inserted by himself in the same page. Of
course the test function is trivial, but how can I trigger it when user
clicks Delete? I could think of 2 possible scenarios:

1) validation-like mechanism: when user clicks Delete the check will be
triggered and if it fails a validation error should be displayed in the row
and the deletion should not be performed. Anyway, how can I implement a
validation mechanism without having any control to be validated? Here it is
not the case of validating e.g. the textbox with the category name to be
updated, but I must check the availability of a command when the user clicks
on a button.

2) another (even better) way could be preventing the datagrid from inserting
the Delete button in the template whenever the row being shown cannot be
deleted according to the specified test. Is this possible?

Is there any way of doing any of the two tasks? Or should I try a totally
different way? Thanks!
 
R

Riki

Dan said:
Hi all, I have a question about datagrid: let's say I have a datagrid
which shows the categories for some items; each category has an ID
field and a description. I use an ASP.NET page with a datagrid bound
to this table to show, edit or delete any row in this table. I have
made a template of the edit/cancel column to be able to check for
invalid values (e.g. an empty or duplicate category), using
validators referring to the edit control of my edit template.

All this works fine, my question is: I'd like to Disable the delete
command for all the rows which are assigned to some items in the
database, so that it will not be possible to delete a category
without first modifying all the items assigned to it. Anyway, the
user will be able to delete unused categories, either existing or
inserted by himself in the same page. Of course the test function is
trivial, but how can I trigger it when user clicks Delete? I could
think of 2 possible scenarios:
1) validation-like mechanism: when user clicks Delete the check will
be triggered and if it fails a validation error should be displayed
in the row and the deletion should not be performed. Anyway, how can
I implement a validation mechanism without having any control to be
validated? Here it is not the case of validating e.g. the textbox
with the category name to be updated, but I must check the
availability of a command when the user clicks on a button.

2) another (even better) way could be preventing the datagrid from
inserting the Delete button in the template whenever the row being
shown cannot be deleted according to the specified test. Is this
possible?
Is there any way of doing any of the two tasks? Or should I try a
totally different way? Thanks!

2) certainly is possible.

If your SQL SELECT command is something like this:
SELECT ID,Category FROM Categories;

change it into something like this:
SELECT ID,Category,(SELECT COUNT(*) FROM Items WHERE
CategoryID=Categories.ID) AS nItems FROM Categories;

Then, bind the Visible property of your delete button like this:
Visible='<%# DataBinder.Eval(Container.DataItem,"nItems")>0 %>'
 

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,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top