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
498 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect, That did it.  Thanks so much for your help, you are great.  Have a great day.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

17 Experts available now in Live!

Get 1:1 Help Now