ASP. NET Common frontend and backend calls, js execution in the background, pop-up modal window, js Modal
◆ Default. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" https://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> No title page </title>
◆ Default. aspx
Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. text; using System. data. sqlClient; public partial class _ Default: System. web. UI. page {public string strVar = string. empty; protected void Page_Load (object sender, EventArgs e) {Button2.Attributes. Add ("onclick", "return confirm ('Are you sure you want to proceed? \ N Click OK to execute the corresponding background method! \ N click the cancel button and no method is executed! ') ");} Private void MsgAlert (string msg) {ClientScriptManager cs = Page. clientScript; StringBuilder sbScript = new StringBuilder (); sbScript. append ("<script language = 'javascript 'Type = 'text/JavaScript '> \ n"); sbScript. append ("<! -- \ N "); sbScript. append ("alert ('" + msg + "');"); sbScript. append ("// --> \ n"); sbScript. append ("</script> \ n"); cs. registerStartupScript (this. getType (), Guid. newGuid (). toString (), sbScript. toString (), false);} protected void button#click (object sender, EventArgs e) {MsgAlert ("this is a common pop-up prompt! ");} Protected void Button2_Click (object sender, EventArgs e) {Response. write ("event method corresponding to the executed button");} protected void Button4_Click (object sender, EventArgs e) {} protected void Button6_Click (object sender, EventArgs e) {Session ["MySession"] = "I am Session";} protected void Button5_Click (object sender, EventArgs e) {} protected void Button7_Click (object sender, EventArgs e) {ClientScriptManager cs = Page. clientScript; cs. registerStartupScript (this. getType (), Guid. newGuid (). toString (), "<script> InvokeByCSMethod ('I am the JS function called by the background function ^_^... '); </script> ", false );} /// <summary> /// function called by the front-end JS function // </summary> /// <param name = "strValue"> </param> // /<returns> </returns> public string InvokeByJS (string strValue, string strValue2) {return (strValue + strValue2);} protected void Button8_Click (object sender, EventArgs e) {} protected void Button9_Click (object sender, EventArgs e) {strVar = "background variable value 1234OK";} public int RunConfirmOper () {try {// SqlConnection conn = new SqlConnection ("Data Source = 192.168.0.20; Persist Security Info = True; initial Catalog = VBADB; User ID = sa; Password = sa "); // conn. open (); // string strSQL = "insert into T1 (name, address) values ('aaaaa', 'bbbbbbbbbb')"; // SqlCommand cmd = new SqlCommand (strSQL, conn); // cmd. executeNonQuery (); // conn. close (); Response. write ("method after confirmation on the execution background"); return 1 ;}catch {return 0 ;}} public int runcancel () {try {// SqlConnection conn = new SqlConnection ("Data Source = 192.168.0.20; Persist Security Info = True; Initial Catalog = VBADB; User ID = sa; Password = sa "); // conn. open (); // string strSQL = "delete T1"; // SqlCommand cmd = new SqlCommand (strSQL, conn); // cmd. executeNonQuery (); // conn. close (); Response. write ("method after canceling execution background"); return 1 ;}catch {return 0 ;}}}
★Tanchu. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Tanchu. aspx. cs" Inherits = "Basic_Tanchu" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" https://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> No title page </title> <script language =" javascript "type =" text/javascript ">/* Pop-up modal form, and the new form is paid by: Pan Xiaoyu 2009-08-15 */function showQueryWindow () {var par1Value = document. getElementById ("TextBox1 "). value; var par2Value = document. getElementById ("TextBox2 "). value; var parValueAll = par1Value + "-" + par2Value; // var getReturnValue is the return value of the modal form. returns the value through returnValue = xxx) var GetReturnValue = window. showModalDialog ('tanchudetail. aspx? Par = '+ parValueAll, '', 'toolbar = no, location = no, directories = no, status = no, menubar = no, scrollbars = yes, resizable = yes, left = 180, top = 60, width = 500, height = 400 '); // var getReturnValue = window. showModalDialog ('tanchudetail. aspx '); // if (getReturnValue! = Null) {var getReturnValuePar = getReturnValue. split (";"); document. getElementById ("TextBox1 "). value = getReturnValuePar [0]; document. getElementById ("TextBox2 "). value = getReturnValuePar [1] ;}</script>
★Tanchu. aspx. cs
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class Basic_Tanchu : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button2_Click(object sender, EventArgs e) { string str1 = TextBox1.Text.Trim(); string str2 = TextBox2.Text.Trim(); string strValueAll = str1 +"-"+ str2; string strSql = @"<script language='javascript' type='text/javascript'>" + @"var getReturnValue = window.showModalDialog('TanchuDetail.aspx?par=" + strValueAll + "');" + @"if(getReturnValue != null){" + @"var getReturnValuePar = getReturnValue.split(';');" + @"document.getElementById('TextBox1').value = getReturnValuePar[0];" + @"document.getElementById('TextBox2').value = getReturnValuePar[1];}" + @"</script>"; RegisterStartupScript("111", strSql); }}
◇ TanchuDetail. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "TanchuDetail. aspx. cs" Inherits = "Basic_TanchuDetail" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" https://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> pop-up webpage </title> <base target =" _ self "/> <script language =" javascript "type = "text/javascript"> function backParentWindow () {var txt1 = document. getElementById ("txt1 "). value; var txt2 = document. getElementById ("txt2 "). value; window. returnValue = txt1 + ';' + txt2; window. close () ;}</script>
◇ TanchuDetail. aspx. cs
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class Basic_TanchuDetail : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string parVar = Convert.ToString(Request.QueryString["par"]); string[] t = parVar.Split('-'); this.txt1.Text = t[0]; this.txt2.Text = t[1]; }}