[ASP.NET] How to call server code in the client

xiaoxiao2021-03-06  68

[ASP.NET] [Original] How to call the server code in the client, such as 3 TEXTBOXs we have on the page, 3 Button, each Button performs different actions. We now want to detect if you press the Enter key in TextBox, if you do different Button calls. That is, press it in TextBox1 to execute the action of Button1, ......

In the test, I found that the server-side code must call the __dopostback function, but this function does not except that the information is generated by the system in the page placed in the page placed in the DataGrid control. (You can see the code by viewing the source file). This way we must hand in the ASPX to add the __dopostback function, and the functions added together have two hidden elements, __ evenettarget, and __eventargument, this is necessary, in fact, .NET is the element name of the event and Parameters are transmitted to, __ evenettarget and __eventargument. Then call the Submit function of the Form to submit back to the server, the server side knows which control is triggered according to the passing parameters, thus calling its corresponding backend code, and then send the new page back to the client. The following is my test page, which use two ways to detect buttons in TextBox Webform2.aspx --------------------------------------------------------------------------- ---------------------------- <% @ page language = "c #" codebehind = "Webform2.aspx.cs" autoeventwireup = "false "Inherits =" UTF8TEST.WEBFORM2 "%> Webform2 </ title> <meta content =" Microsoft Visual Studio .NET 7.1 "name =" GENERATOR "> <meta content =" C # "name =" CODE_LANGUAGE "> <meta content =" javascript "name =" vs_defaultClientScript "> <meta content =" http: // schemas. Microsoft.com/intellisense/ie5 "; name =" vs_targetschema "> <script language =" javascript "Event =" onkeyDown "for =" textbox1 "></p> <p>IF (Event.Keycode == 13) <! - Caution Size -> {__dopostback ('Button1', ''); Return False; <! - It is important, but you will choose button1 for submission->} </ script> <script language = "javascript" Event = "onkeydown" for = "textbox2"></p> <p>IF (Event.Keycode == 13) {__dopostback ('Button2', ''); return false; <! - It is important, but you will choose Button1 for submission ->} </ script> <script language = "JavaScript > Function KeyPress () {ney (Event.Keycode == 13) {__dopostback ('Button3', ''); Event.KeyCode = 0; <! - It is important, otherwise you will choose button1 for submission -> Return False; <! - Otherwise, button1 will be selected for submission ->}} </ script> </ head> <body ms_positioning = "gridLayout> <form id =" form1 "method =" post "runat = "server"> <input type = "hidden" name = "__ eventtarget"> <input type = "hidden" name = "__ evenetargument"> <script language = "javascript" type = "text / javascript"> <! - function __doPostBack (eventTarget, eventArgument) {var theform; if (window.navigator.appName.toLowerCase () indexOf ( "microsoft")> -1.) {theform = document.Form1;} else {theform = document.forms [ "Form1 "];} Theform .__ evenettarget.value = eventtarget.split (" $ "). Join (": "); Theform .__ Eventargument.Value = Eventargument; theform.submit ();} // -> </ script> <asp: button id = "Button1 "Style =" z-index: 101; Left: 192px; position: absolute; top: 88px "runat =" server "text =" button1 "> </ asp: button> <ask: textBox id =" textbox1 "style = Z-Index: 102; Left: 16px; Position: absolute; top: 88px "Runat =" server "> </ asp: textbox> <askTON ID =" Button2 "style ="</p> <p>Z-Index: 103; Left: 192px; position: absolute; top: 120px "Runat =" server "text =" button2 "> </ ask: button> <ask: textbox id =" textbox2 "style =" Z-Index : 104; Left: 16px; Position: absolute; top: 120px "Runat =" server "> </ asp: textbox> <ask: textbox id =" textBox3 "style =" z-index: 105; Left: 16px; position : Absolute; Top: 152px "Runat =" Server "> </ asp: textbox> <asp: button id =" Button3 "style =" z-index: 106; Left: 192px; position: absolute; top: 152px "Runat = "Server" text = "Button3"> </ asp: button> <ask: label id = "label1" style = "z-index: 107; Left: 24px; position: absolute; top: 56px" runat = "Server "> </ ask: Label> </ form> </ body> </ html> Webform2.aspx.cs ------------------------- --------------------------------------------- Using System; Using System .Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web .Ui.htmlControls;</p> <p>A summary description of Namespace utf8test {/// <summary> /// WebForm2. /// </ summary> public class WebForm2: System.Web.UI.Page {protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI. WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.TextBox TextBox3; protected System.Web.UI.WebControls.Button Button3; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls. Button Button1; Private Void Page_Load (Object Sender, System.EventArgs E) {// Place the user code here to initialize the page TextBox3.attributes.add ("onkeypress", "keypress ()"); // pay attention to the case} #Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// </ summary> private void InitializeComponent () {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.Button2.Click = new System.EventHandler (this.Button2_Click); this.Button3 .Click = new system.eventhandler (this.button3_click; this.load = new system.eventhandler (this.page_load);</p> <p>} #Endregion</p> <p>Private void button1_click (object sender, system.eventargs e) {label1.text = "1";</p> <p>Private void button2_click (object sender, system.eventargs e) {label1.text = "2";</p> <p>Private void button3_click (object sender, system.eventargs e) {label1.text = "3";}}}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-90631.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="90631" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.040</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'nECeIsX_2FguLenmZGwfS0F_2FgOYng1yC8Rwr8_2Fmo1JvK0cu_2B72NdQAm8MizDTB5HUutx6Kn3ZWrFFTBTJtTV_2B0cQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>