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
517 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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