By var JS = confirm ("Confirm operation ?"); Technical difficulties

Source: Internet
Author: User
Tags call back

When I see the title of this article, some may be confused. Will confirm cause technical difficulties? Is it a small problem? What I want to talk about here is not a small problem, but a technical problem caused by the use of confirm. Some may be confused. Isn't confirm a confirmation dialog box displayed? There are only two options for users: click "yes" or "no". Some may say: in addition to "yes" and "no", there are also points "X ". yes, there is no error. Confirm can operate on these three types, while "X" execution is disabled and "no" is the same. OK. Back to the topic. Since confirm is used to confirm the operation of a user's project, what kind of technical difficulties will it cause?

Before continuing to discuss this technical problem, I would like to thank the two enthusiastic comrades in the garden, one being the bag and the other being the big V. as for the above question, I have discussed a lot with the bag, and then the big V, even late at night. I mentioned this question to him, and he still had to rest and discuss it with me. after discussion with the two masters, they both had different opinions and ideas, but they still did not find a perfect solution. ^. ^ at a break in the early morning, Jack said, "I'm going to do an experiment and study. ", huh, I hope Big V can work out a good solution, and then instruct me to solve this problem in the project. Thank you first. this problem may have been encountered by many friends recently, and you may encounter it soon after reading this article.

Where is the problem?
As a friend of winform, no one knows the MessageBox. Show () method. Good, in winProgramIs to use it for user confirmation, such:

Yes, the above is the MessageBox prompt that we use very well. So we can do this in web development, and then execute the corresponding business operations according to the user's choice? Speaking of the Web, you may immediately think of using "window. Confirm ()" for the Operation prompt, good, first look:

As shown in, have you found any problems? A simple application example. for example, in a business system card, the current card reads a piece of data and displays it on the card. At this time, the user has modified part of the data that can be sliced, next, When you click Create directly without clicking save, you should prompt the user "the current data has been modified. Are you sure you want to save it? ", Perform corresponding operations based on the user's selection (YES: Save the current data, clear the value of the interface control element, insert new data; no: do not save the current data and directly enter the new status .).
InCodeFor example, we need to develop a business document (such as a purchase/sales order). In some cases, we need to confirm with the user before proceeding with the subsequent operations, input JS Code (string JS = "window. confirm ('........ ') "), the result is used to store the return value of the confirmation dialog box. You can choose to execute an operation according to the user's choice. This requirement can be implemented easily in winfrom, confirm is a client-only method. how can this function be implemented in Web development ??

The answer is that it cannot be implemented. After multiple experiments, it is notified of failure because the return value of the client's JavaScript method cannot be obtained in the C # code of the server.

 

Conjecture cannot solve the problem

Here we can try to make an unsuccessful experiment (because I tried it). Before the experiment, I had such a conjecture that we passed scriptmanager in the C # code of the server. registerclientscriptblock (); Method to execute the client's JavaScript method. In this client method, we call ASP. the pagemethos method supported by net Ajax, page. CS defines a static variable. When the server's registerclientscriptblock is executed, the specified client method is called. This method calls back the page. in this way, the pagemethos in CS will return the confirmation operation;

1 Public   Partial   Class Productcard: system. Web. UI. Page
2 {
3 Private   Static   String Key =   String . Empty;
4 Protected   Void Page_load ( Object Sender, eventargs E)
5 {
6 If ( ! Ispostback)
7 {

9}
10 }
11
12 Protected   Void Button#click ( Object Sender, eventargs E)
13 {
14 Scriptmanager. registerclientscriptblock ( This , This . GetType (), " JS " , " Test () " , True );
15 If (Key =   " True " )
16 {}
17 Else
18 {}
19 }
20
21 [Webmethod]
22 Public   Static   Void Getoperationresult ( String Result)
23 {
24Key=Result;
25}
26 }

 

1 < ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" Enablepagemethods = "True" >
2 </ ASP: scriptmanager >
3 < Script Type = "Text/JavaScript" >
4 Function Test ()
5 {
6 VaR JS =   " Confirm ('OK? ') " ;
7 Pagemethos. getoperationresult (JS. tostring ());
8 }
9 </ Script >

Can this solve the problem? No. although it is a client method executed in a button event, the user method can call back the pagemethos method of the server, which seems to be able to return data. yes, it can return data, but it still cannot meet our needs. during the experiment, we found that the confirmation dialog box will pop up only when the button-based implementation method (button#click () exits ........., the experiment proves that conjecture is a conjecture and the problem still cannot be solved.

New problems caused by weird ideas
Suddenly I thought of a very strange way. I tried and guessed it. OK. How did I do it? Since MessageBox in winform can be used for prompting, can it be used in the web? MessageBox is encapsulated in system. window. in forms. window. forms. DLL is introduced to the web project, and then in ASP. net to confirm the prompt. the idea is that the experiment is successful, so now is the channel ready? Not necessarily, when using system. window. forms MessageBox is used as a confirmation prompt to solve ASP.. net. window. forms MessageBox in ASP. net cannot be implemented"Mode", Now it seems that the system is not introduced again. window. forms MessageBox to solve the problem at the beginning of confirmation prompt. the first figure in this article is in ASP. the effect of using MessageBox in. NET is as follows:

 

Is the simple question complicated?

Maybe if you are reading this article, you may say that I have a problem with my head and make a simple question so complicated. Well, have you ever thought about the importance of this function? I personally think this function is very important, especially when there is a complex business relationship in the system, a wrong operation may cause XXX. prompting users is necessary and important.

Since winfrom can easily implement this function, Web implementation is so complicated, this may be the reason why the Web itself is based on the request and response mode. ^. ^ However, I hope that the control expert can develop such a control and look forward to ing .......

 

I think again

Window. confirm () can only be executed on the user end, and the return value can be easily obtained. programmers can also conveniently use this return value for corresponding business processing. isn't JavaScript OO? To some extent, it is much more convenient, and some time ago it was still very fierce. Based on the above reasons, imagine we can take the business out of the customer to do it? The client is used for processing. Common methods are encapsulated by JS, and ASP. net Ajax provides powerful functions that can communicate with the server from time to time. can we change the development mode to put the usual CS service processing on the page into the client's JavaScript processing?
This method is actually feasible. although feasible, there are many problems, such as higher requirements for Team developers (client JavaScript, Ajax, etc.), reducing development efficiency and affecting development progress, the logic code of the page cannot be encapsulated perfectly.

 

Where to go
A teacher often said to me: "You don't want to tell me what you want to do or what you want to do. You just need to tell me whether you can do it. ", can this problem be solved in this article? I can't do it. Maybe my friend from CS to BS development knows this best. this is not a problem for our technical staff, but a defect of the Web itself. the problem is that we cannot ignore it all the time. We always need to provide a solution. In this case, do we give up the confirmation prompt function? For me, I only gave up.
This article ends. My dear friend, you may be about to close this page. You have encountered the same problem. How did you deal with it? Is there any good solution? I hope I can guide my later students to learn. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.