.net中前臺javascript與後臺C#交互

.net中前臺javascript與後臺C#交互javascript

如何在JavaScript訪問後臺C#方法、變量java

方法一:使用Ajax開發框架,後臺方法定義前添加[AjaxPro.AjaxMethod],而後就能夠在前臺js腳本中調用後臺C#函數。框架

方法二:後臺方法聲明爲public或者protected,而後前臺使用js腳本進行調用。函數

.csthis

public string Str().net

     {server

         return "javaScript函數中執行後臺C#方法..";ip

}開發

.aspxget

<script type="text/javascript">

         var a = "<%=Str()%>";

     alert(a);

</script>

方法三:使用Session變量

.cs

if (Session["siteName"] == null)//判斷是否存在指定Key值的Session變量

                 Session["siteName"] = "";//若是不存在則建立Session變量

             //給Session["siteName"]變量賦值

.aspx

var siteName="<%=Session["siteName"] %>";

方法四

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" style="display:none;"/>

<input type="button" id="btnSave " value="保存" onclick="GetBehind ();return false;" />

.aspx.cs

     <script type="text/javascript">

  //調用後臺方法

//使用這種方式調用後臺代碼,能夠在調用以前在前臺作一些驗證

         function GetBehind() {

                 document.getElementById("Button1").click();

         }

</script>

.cs

protected void Button1_Click(object sender, EventArgs e)

{}

如何在後臺C#代碼中執行javaScript函數

方法一:void Page.RegisterStartupScript(string key,string script);

方法二:使用隱藏域或者Literal控件,在前臺使用js腳本把一些js函數控制的值寫進隱藏域或者Literal控件,而後前臺使用Hidden.Value或者Literal.Text讀取前臺值。

.aspx

function GetTitleID(obj)

{

     sTitleID=obj

     if(sTitleID!=null)

         document.getElementById("HiddenField1").value=type+','+sTitleID;

     else

         document.getElementById("HiddenField1").value=type+',0';

}

.cs

string hiddenValue = this.HiddenField1.Value;

相關文章
相關標籤/搜索