Solved

Conditional Deleting of a GridView row based on field Value in the currrent row being deleted.  ASP 2.0 Framework

Posted on 2006-11-14
8
499 Views
Last Modified: 2008-01-09
Scenario:
platform: .NET 2.0 Framework

I have a gridview with the command field that has the Edit and Delete commands in that column.   I want to be able to make a conditional statement when the delete command is fired to check a field in the row for the condition
IF value = "Owner" then don't delete, and set a label message box to alert user that Deleting an Owner is not allowed.

So I believe I can use the RowDeleting Event handler to do this, but am not sure.   So I need to know the following.

1. How to reference the Templated field name in the RowDeleting Event (Below I have some code I used in the RowDataBound event, I have tried to use this same code in the Event but it doesn't seem to be able to get at the control)  
2. What code would I use to bail out of the delete function if that field is = "Owner"

Here is some code I have that

                Label lblGetRole = (Label)e.Row.FindControl("lblRole");  // The column is a templated field this is when in non Editing mode.
                DropDownList ddlChangeRole = (DropDownList)e.Row.FindControl("DropDownListRole");   //This is the field in the Template, it becomes a dropdownlist

Any help would be appreciated.

               
0
Comment
Question by:jholmes724
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17941135
You can get the row index from the event argument:

  e.RowIndex

Get a reference to the specific row in the DataTable, and get the value for Owner, and add an If condition to delete the row when role is not "Owner".

Bob
0
 

Author Comment

by:jholmes724
ID: 17942431
That really doesn't answer my question.  I am not as concerned with finding the value as I am in understanding where to put the code and what the code should look like.  I want to know if I am correct in thinking that this conditional logic needs to happen in the RowDeleting Event handler of the Gridview,  and if so I can add the If statement to check for the value of Owner, but I am unclear as to how these event pairs work on the Gridview.  You have the RowDeleted, and the RowDeleting.  It is my understanding tha tthe RowDeleted event is where you would write logic to handle some condition after the delete happens, and the RowDeleting is where you would put code if you wanted to add logic to occur before the event.

So I am thinking I would have if statement in the RowDeleting Event handler that would look something like this

Label lblGetRole = (Label)e.Row.FindControl("lblRole");   // Get value of "Owner" from field in current row
  if (lblGetRole.Text == "Owner")
             ??? Bail out of Delete in progress and write notification to user in textbox ..... lblErrorMsg.Text  = "Cannot Delete Owner"

I am hoping I am making myself more clear.   Can  you provide more detail?  

Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17946204
Hopefully this will help:

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Get value of "Owner" from field in current row
        Label lblGetRole = (Label)e.Row.FindControl("lblRole");
        if (lblGetRole.Text == "Owner")
        {
            this.lblErrorMsg.Text = "Cannot Delete Owner";
            // Don't allow the row to be deleted
            e.Cancel = true;
        }
    }

Bob
0
 

Author Comment

by:jholmes724
ID: 17946413
Thanks, I am getting close.  I have one more issue I am hoping you can help with,  Here is my code:

protected void  GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // Cancel the delete operation if the user attempts to remove
    // the Owner from the GridView control.
    Label lblGetRole = (Label)e.Row.FindControl("lblRole");

    if (lblGetRole.Text == "Owner")
    {

        e.Cancel = true;
        lblDeleteError.Text = "Cannot Delete Owner.";

    }
}

Problem: I get the following Error on this line of Code:  Label lblGetRole = (Label)e.Row.FindControl("lblRole");
Error Message:  Error      62      'System.Web.UI.WebControls.GridViewDeleteEventArgs' does not contain a definition for 'Row'

I know I have to create an instance of my GridView row, I have tried a few things but still cannot get it to work.  What is the code I need to get it to view the Row?

Thanks again.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17946485
Yeah, that was untested, uncompiled code before (I am not fully awake this morning)  :(

Hopefully this is better:

        GridView grid = (GridView)sender;
        GridViewRow row = (GridViewRow)grid.Items[e.RowIndex];
        Label lblGetRole = (Label)row.FindControl("lblRole");

        if (lblGetRole.Text == "Owner")
        {

            e.Cancel = true;
            lblDeleteError.Text = "Cannot Delete Owner.";

        }

Bob

0
 

Author Comment

by:jholmes724
ID: 17946572
No problem, I just appreciate the help and quick response.  Ok I now am getting the following error on the Index.

Here is my modified code ;  (Gridview ID is GridView1)
 
protected void  GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    GridView grid = (GridView)sender;
    GridViewRow row = (GridViewRow)grid.Items[e.RowIndex];
    Label lblGetRole = (Label)row.FindControl("lblRole");

    if (lblGetRole.Text == "Owner")
    {

        e.Cancel = true;
        lblDeleteError.Text = "Cannot Delete Owner.";

    }
}

Code where error is happening: GridViewRow row = (GridViewRow)grid.Items[e.RowIndex];  (Error is on Items)

Error      62      'System.Web.UI.WebControls.GridView' does not contain a definition for 'Items'      
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 100 total points
ID: 17946671
This is just too funny--I have a working 2.0 web site where I can play around with code, and I had a web page named GridView, which conflicted with the GridView web control, but it didn't give me any compile errors on that code.  When I renamed the web page, I finally got the error that you are talking about.

Try this:

        // Cancel the delete operation if the user attempts to remove
        // the Owner from the GridView control.
        GridView grid = (GridView)sender;
        GridViewRow row = (GridViewRow)grid.Rows[e.RowIndex];
        Label lblGetRole = (Label)row.FindControl("lblRole");

        if (lblGetRole.Text == "Owner")
        {
            e.Cancel = true;
            lblDeleteError.Text = "Cannot Delete Owner.";
        }

Bob
0
 

Author Comment

by:jholmes724
ID: 17946740
Perfect, That did it.  Thanks so much for your help, you are great.  Have a great day.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now