Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
522 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

670 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