2012年11月15日 星期四

ASP C#如何抓javascript變數





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Aspx_postback.WebForm2" %>   

  <!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 id="Head1"   runat="server"   >   
                 <title   > Untitled   Page    </title   >   
                 <script   type="text/javascript"   >   
               
                   var   pa1   =   "Time   is"   ;  
                   function   go(){                           
                          PageMethods.cs(pa1,js);   //PageMethods.c#方法(c#接收的值,叫javascript另一方法)                      }  
                     
                   function   js(cs)  
                   {  
                         alert(cs);  
                   }  
                           

                 </script>   
  </head>   
  <body>   
                 <form  id="form2"   runat="server"   >   
                 <div>   
                             <input   type="button"   onclick="go();"   value="submits"   />   
                             <asp: ScriptManager   ID="ScriptManager1"   runat="server"   EnablePageMethods="true"/>   
                 </div>   
                 </form>   
  </body>   
  </html>   
==================================================
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;  
namespace Aspx_postback
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [System.Web.Services.WebMethod]
        public static string cs(string pa1)
        {
            return pa1 + DateTime.Now.ToString();
        }  
    }
}



=======================

換行符號
Page.RegisterStartupScript("scriptStr", "<script language='javascript'>alert('"+strMsg+"'\\n换行);</script>");

在JS里,换行是\n,在C#里,就需要两个“\\”来表示一个“\”
“\”在C#中是转义字符

0 意見:

張貼留言

 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template