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
504 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Signal R -send data to client: only if they are allowed to see it ? 1 57
ASP.NET MVC identity 6 30
asp.net mvc5 6 23
VS 2015 is giving [No relevant source lines] 2 30
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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