Recently, I am working on the Add function. I need to determine whether a certain item has been added, and then give a prompt to the user to determine whether to continue adding based on the user's selection result. the confirm dialog box of JS is used to implement this function, but the problem is how to obtain the confirm result. After all, the result is triggered on the client and must be sent back to the server. I checked some information on the Internet, said to use a hidden htmlbutton, and then call _ dopostback to trigger it on the client. This method is indeed feasible, but the premise is that no parameter needs to be transferred, that is, the second parameter of _ dopostback cannot be used, and cannot be obtained even if given. in fact, this parameter is obtained through the control itself. For example, if you want to use the click event, you cannot obtain the parameter externally. the control actually inherits ipostbackeventhandler, and then obtains this parameter in raisepostbackevent. Therefore, if you want to implement the re-sending and obtain different parameters, the problem can only be solved by the page ipostbackeventhandler interface.
The solution is as follows:
1. Set enableeventvalidation = "false" in the page command; otherwise, an error will be prompted. For details, see msdn
2. inherit the ipostbackeventhandler interface from page to implement the public void raisepostbackevent (string eventargument) method. Code1
1 Public Void Raisepostbackevent ( String Eventargument)
2 {
3 Using (System. Io. streamwriter SW = New System. Io. streamwriter ( " E: \ test.txt " , True ))
4 {
5Sw. writeline (eventargument );
6}
7 }
3. register the send-back script as needed and click OK to execute the logic in raisepostbackevent. jscancel is used to trigger a send-back event that is a hidden button, such as canceling the operation. Registerpostbackscript
1 String Jscontinue = "" ;
2 String Jscancel = Clientscript. getpostbackeventreference (btncancel, "" );
3 String Jsconfirm = "" ;
4 If ( ! Ispostback)
5 {
6 For ( Int I = 0 ; I < 10 ; I ++ )
7 {
8 If (I % 2 = 0 )
9 {
// Register the script to continue the operation, trigger the page sending event, and pass the parameter I
10 Jscontinue = Clientscript. getpostbackeventreference ( This , I. tostring ());
11 Jsconfirm = String . Format ( " If (confirm ('continue operation') {0}; else {1 }; " , Jscontinue, jscancel );
12 Clientscript. registerclientscriptblock (GetType (), guid. newguid (). tostring (), jsconfirm, True );
13 }
14 }
15 }
In addition, clientscript registers the registerclientscriptblock script after all servers are executed.CodeBefore executing the client script,
Instead of executing a confirm cyclically at a time as imagined, some business logic may be limited.
Of course there are still many solutions. You can also consider using Ajax technology to learn ......