Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

How to intercept gridview's Update event and alert user of required field if needed?

Hi, I'm using vs2012, C# and asp.net.
When a user updates a row, there could be 4 blank cells but when user checks a checkbox column then click Update, I would like to cancel the update and alert message to user that the 4 blank cells need to have text if they check that checkbox.  I would only process the update if all 4 blank cells are no longer blanks.

Can this be done and how in C#/asp.net or this has to be done in javascript or jQuery and how?  Thank you.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

are you asking for:

if <  checks a checkbox == true >
            NO update BUT message
else
      do update


or

if <  checks a checkbox == true > and <all 4 boxes filled in>
            NO update BUT message
else
        do update

Also, how are you populating the gridview? Or, what is your update routine called?
Avatar of lapucca
lapucca

ASKER

Hi, The gridview is databind to a "List<UserPrincipal>" objects.

what is your update routine called? ==>>  grid_RowUpdating

All the columns discussed here are bounddata.  Do i need to convert them to template type?

When user clicks the update button in grid after editing,
if ((user checks the checkbox cell) &&  blankOrNull (Col3 or col4 or Col5))
         {
             Cancel the Update,
             stays in Edit mode,
             alert message to user that these 3 columns cannot be blank
}
else
       Continue with Update.
try (requires some syntax work):

if ((user checks the checkbox cell) &&   (Col3 != null  && col4 != null && Col5  != null))
         {
            // Cancel the Update,
            // stays in Edit mode,
             alert message to user that these 3 columns cannot be blank
}
else
{
       //Continue with Update.
      grid_RowUpdating();  
}

Basically, with bound gv like this, if we bypass GvBindingSource.EndEdit();, update will not happen. If we by pass it, it will be the same as canceling it.

.EndEdit()   <-- sends data from the gv to dataset.
.UpdateAll(datasetNameYouHave);  <-- sends data from dataset to database

If you want I can build a sample to test and give you the exact syntax. Let me know. I can work on it this evening.

This post has been revised...
Avatar of lapucca

ASKER

I think I'm able to do all that I need to do in the GridView.RowUpdating Event and I know how to it except for the part after I cancel the update, how do I alert the user?
Thank you.
You need to add the following in your source file:
<head runat="server">
   <script type="text/javascript">

    function showMsg() 
   {
      alert('With checkbox cell checked, col3, col4, and col 5 need to be filled in');
   }

 </script>
</head>

Open in new window


Also in your code, you add something like:

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "showMsg(1)", true);

Now I will build a sample to show the exact location where you need to put this code.

Mike
Avatar of lapucca

ASKER

Sounds good, thank you.
btw, remove 1 from showMsg(1)

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "showMsg()", true)

I am testing it now.

This is what I got as expected:

User generated image
Avatar of lapucca

ASKER

Looks good.  Do I put this loc, Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "showMsg()", true)
,  in C# where I want to call the pop up alert?
lapucca,

Some background information. Your gv is bound control and you have possibly have used a wizard to drop this gv on to the page.

In doing so, there must be the following commands some where:

.EndEdit()   <-- sends data from the gv to dataset.
.UpdateAll(datasetNameYouHave);  <-- sends data from dataset to database


if ((user checks the checkbox cell) &&   (Col3 != null  && col4 != null && Col5  != null))
         {
            // Cancel the Update,
            // stays in Edit mode,
            Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "showMsg()", true)
          // there is no need to cancel a thing.  What you can do is to determine which cell is left blank set focus on that.
}
else
{
       //Continue with Update.
      grid_RowUpdating();  
}

Is  grid_RowUpdating(); somehow wired to execute EndEdit()  and UpdateAll()?

My focus
You need to do some syntax work on it:

if ((user checks the checkbox cell) &&   (Col3 != null  && col4 != null && Col5  != null))
         {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "showMsg()", true)
           if(Col3 != null)
           {
                Col3.Focus();
           }
            elseif if(Col4 != null)
           {
              Col4.Focus();
          }
            elseif if(Col5 != null)
           {
              Col5.Focus();
          }
}
Avatar of lapucca

ASKER

Hi,
I'm just canceling the grid_RowUpdating event.  I'm hoping it will remain in Edit mode.

What do I put for "CallMyFunction"?  
"showMsg()", is my function name, got that.  

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

After looking at a couple of example on net, https://msdn.microsoft.com/en-us/library/bahh2fef(v=vs.110).aspx 
and https://books.google.com/books?id=yd5MVvN_5_IC&pg=PA327&lpg=PA327&dq=e.Row.RowState&source=bl&ots=iIjx3gUr6e&sig=YVHcrxI8kG_ltIRPiNuvLqTlv8M&hl=en&sa=X&ei=HlrSVPv-M4nYoASmooHgBA&ved=0CJQBEOgBMBI#v=onepage&q=e.Row.RowState&f=false

I think this is just registering javascript code from back end code.  The Javascript has to be tied to some Server Control Event action for this.  I need to be able to call just an Alert function from code behind after manually validating any of the 4 cells on the gridview contorl doesn't have data.  I don't think this will do that.  Any other way to achieve this?
Thank you.
Are you able to get the message box to popup? If so, then that should be it I suppose.

Mike
Avatar of lapucca

ASKER

Ha!  Last part of this page, http://stackoverflow.com/questions/5731224/calling-javascript-function-from-codebehind 
gave me a great idea that is such a simple solution!  I'll just use a hidden label and from code behind after checking the required field, I'll just enable that label with message if the fields are not completed and cancel the RowUpdating event!  Thank you.
Good luck with the rest of your project. Yes, as the saying goes there are many ways to skin an apple (I like cats).

I see that the link is also using "CallMyFunction" for the key. The code I gave to you was from one of my old projects I did last summer. I probably had the code sample from the same place.
Avatar of lapucca

ASKER

Lol...
I think, last summer someone helped me with this project. He must be the same person also active in that site. That site didn't have the html portion but I had it in my project from the summer.

Basically, no one inventing these codes. We all are copying from one another. Because you asked this question, I got a chance to review it again and I thank you for that.

Mike