ASP.NET 常用前后台调用、后台执行js、弹出模态窗口

◆Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>    
</head>
<body>
<script language="javascript" type="text/javascript">
/*
    c# 常用前后台互相调用方法,示例程序
    整理人:潘晓宇 20090814
*/
//普通无参js函数
function AlertFunction()
{
alert("通过OnClientClick方法调用普通无参js函数成功!");
}


//普通有参js函数
function TestWithPar(msg)
{    
    var t = "成功!";
alert(msg+t);
}


//通过前台JS函数读取Session
function GetCSSessionValue()
{
    var mySessionValue = '<%=Session["MySession"]%>';
    alert(mySessionValue);
}


//被后台函数调用的JS函数
function InvokeByCSMethod( msg )
{
    alert(msg);
}


//JS函数调用后台函数
function InvokeCSMethod(aa,bb)
{
    //var a = '<%=InvokeByJS("这是通过前台JS函数调用后台CS中函数返回的结果!")%>';
    var a = '<%=InvokeByJS("' + aa + '","' + bb + '")%>';
    alert(a); 
}


//取得CS中的变量 值
function GetCSVarValue()
{
    var vv ='<%=strVar%>';
    alert(vv);
}


//判断Confirm对话框点击了那个按钮
function ConfirmCaseOperate()
 {
  var retVal = confirm("确认吗?")
  if (retVal)
  {
     alert("点击了确认按钮");    
  }
  else
  {
        alert("点击了取消按钮");                
  }
 }
</script>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="普通提示信息" /><br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Confirm对话框" /><br />
        <asp:Button ID="Button11" runat="server" Text="Confirm对话框接收不同的操作" OnClientClick="return ConfirmCaseOperate();" /><br />
        <br />
        <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="通过OnClientClick方法调用普通无参js函数" OnClientClick="return AlertFunction();" />
        <br/>
        <asp:Button ID="Button3" runat="server" Text="通过OnClientClick方法调用普通有参js函数" OnClientClick="TestWithPar('通过OnClientClick方法调用普通有参js函数');" /><br />
        &nbsp;<br />
        <asp:Button ID="Button7" runat="server" OnClick="Button7_Click" Text="后台调用前台JS函数" /><br />
        <asp:Button ID="Button8" runat="server" Text="前台JS函数调用CS中普通函数" OnClick="Button8_Click" OnClientClick="returnInvokeCSMethod();" /><br />
        <br />
        <asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="后台设定Session" /><asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="前台JS函数读取Session" OnClientClick="return GetCSSessionValue();" /><br />
        <asp:Button ID="Button9" runat="server" OnClick="Button9_Click" Text="为后台变量付值" />
        <asp:Button ID="Button10" runat="server" Text="JS函数取得后台变量的值" OnClientClick="return GetCSVarValue();" /><br />
        &nbsp;
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        </div>
    </form>
</body>
</html>
◆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('确认进行吗?\\n点击【确定】按钮后,执行后台对应方法!\\n点击【取消】按钮后,不执行任何方法!')");
    }


    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 Button1_Click(object sender, EventArgs e)
    {
        MsgAlert("这是普通弹出提示信息!");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("执行了按钮对应的事件方法");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {


    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        Session["MySession"] = "我是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('我是被后台函数调用的JS函数 ^_^...');</script>", false);
    }


    /// <summary>
    /// 被前台JS函数调用的函数
    /// </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 = "后台变量值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','bbbbbbbbb')";
            //SqlCommand cmd = new SqlCommand(strSQL, conn);
            //cmd.ExecuteNonQuery();
            //conn.Close();
            Response.Write("执行后台确认后的方法");
            return 1;
        }
        catch
        {
            return 0;
        }
        
    }


    public int RunCancelOper()
    {
        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("执行后台取消后的方法");
            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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language="javascript" type="text/javascript">
    /*
        弹出模态窗体,并向新窗体付值
        整理人:潘晓宇 2009-08-15
    */
        function showQueryWindow()
        {
            var par1Value = document.getElementById("TextBox1").value;
            var par2Value = document.getElementById("TextBox2").value;
            var parValueAll = par1Value +"-"+par2Value;
            
            //var getReturnValue 为模态窗体返回值(模态窗体通过window.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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        弹出画面,在弹出画面中选择信息后附到原始画面中<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="js弹出" OnClientClick="showQueryWindow();" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="后台弹出" /></div>
    </form>
</body>
</html>
★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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>被弹出的网页</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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%; background-color:Green; vertical-align:top;">
            <tr>
                <td style="text-align: left;" colspan="3" rowspan="3">
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="txt1" runat="server"></asp:TextBox>&nbsp;<br />
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" BorderColor="#FFC0C0" Text="确定" OnClientClick="backParentWindow();" /></td>
            </tr>
            <tr>
            </tr>
            <tr>
            </tr>
        </table>
        &nbsp;
    </div>
    </form>
</body>
</html>
◇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];
    }
}
InvokeCSMethod
InvokeCSMethod
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jsjpanxiaoyu/article/details/54564614
个人分类: .NET c#.net
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭