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

xiaoxiao2021-03-06  69

[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, pressing the back to the bus in TEXTBOX1 to perform the action of Button1, ... Test, I found that the server-side code must call the __dopostback function, but the function is in addition to the system in the page placed with the DataGrid control. Outside, other pages do not exist. (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 "> if (event.keycode == 13) <! - Check - -> {__dopostback ('Button1', ''); Return False; <! - It is important, but you will choose button1 for submission ->} </ script> <script language = "javascript" Event = "onkeydown" for = "TextBox2"> if (event.keycode == 13) {__dopostback ('Button2', ''); Return False; <! - It is important, but you will choose Button1 for submission ->} </ script> < Script language = "JavaScript T "> function keypress () {i (Event.keycode == 13) {__dopostback ('Button3', ''); Event.KeyCode = 0; <! - It is important, but you will choose button1 for submission -> Return False; <! - Otherwise, Button1 will be selected to submit ->}} </ script> </ head> <body ms_positioning = "</p> <p>GridLayout> <form id = "form1" method = "post" runat = "server"> <input type = "hidden" name = "__ evenettarget"> <input type = "hidden" name = "__ evenetargument"> <script language = "JavaScript" type = "text / javascript"> <! - function __dopostback {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> <ask: button id = "button1" style = "z-index: 101; Left: 192px; position: absolute; top: 88px" runat = " Server "Text =" Button1 "> </ asp: button> <askBOX1" style = "Z-Index: 102; Left: 16px; position: absolute; top: 88px" runat = "server"> </ asp: textbox> <ask = "button2" style = "z-index: 103; Left: 192px; position: absolute; top: 120px" runat = "server" text = "button2"> </ asp : Button> <asp: textbox id = "t EXTBOX2 "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> <ask: button id =" Button3 "style ="</p> <p>Z-Index: 106; Left: 192px; position: absolute; top: 152px "Runat =" server "text =" button3 "> </ ask: button> <ask: label id =" style "style =" z-index : 107; Left: 24px; Position: absolute; top: 56px "Runat =" server "> </ ask: label> </ form> </ body> </ html> Webform2.aspx.cs ------ -------------------------------------------------- -------------- USING SYSTEM; Using System.comPonentModel; Using System.Data; Using System.drawing; Using System.Web; Using System.Web.SessionState; Using System.Web.SessionState; use System.Web.ui; Using System.Web.ui.WebControls; Using System.Web.ui.htmlControls; Namespace UTF8TEST {/// <summary> /// Webform2 Summation Description. /// </ 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 . U.WebControls.textBox textBox3; protected system.web.ui.webcontrols.button button3; protected system.web.ui.webcontrols.label lab el1; protected System.Web.UI.WebControls.Button Button1; private void Page_Load (object sender, System.EventArgs e) {// Put user code to initialize the page here TextBox3.Attributes.Add ( "onkeypress", "keypress () "); // Watch the case} #Region Web Form Designer Generated Code Override Protected Void OnInit (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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-90031.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="90031" 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.043</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 = '1WF4neOW1raq7k6hFC0zGcSR_2B9iB4zf36YvWu4QaeHVkTnn53WVY4jCVFj_2BUz9U1chRCa87PkJWNlzVx'; 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>