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
Solved

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

Posted on 2015-02-02
18
156 Views
Last Modified: 2015-02-05
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.
0
Comment
Question by:lapucca
  • 10
  • 8
18 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40584675
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?
0
 

Author Comment

by:lapucca
ID: 40584751
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.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40584783
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...
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:lapucca
ID: 40584864
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.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40585575
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
0
 

Author Comment

by:lapucca
ID: 40587239
Sounds good, thank you.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40587271
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:

message
0
 

Author Comment

by:lapucca
ID: 40587289
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?
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40587453
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
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40587466
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();
          }
}
0
 

Author Comment

by:lapucca
ID: 40587646
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.
0
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40587672
"CallMyFunction" is key
Type: System.String
The key of the client script to register.

A client script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

See: https://msdn.microsoft.com/en-us/library/btf44dc9%28v=vs.110%29.aspx

I do not have a good grasp of it. I suppose with too many scripts, they could be better handle if they are registered. If you find more about this please let me know about it so I get to lean it too.
0
 

Author Comment

by:lapucca
ID: 40589769
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.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40589781
Are you able to get the message box to popup? If so, then that should be it I suppose.

Mike
0
 

Author Closing Comment

by:lapucca
ID: 40589835
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.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40589866
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.
0
 

Author Comment

by:lapucca
ID: 40592090
Lol...
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40592141
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 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