簡體   English   中英

從帶有參數的javascript調用函數后面的代碼

[英]call code behind function from javascript with parameters

我在.ascxUser Control )中具有代碼隱藏功能,如下所示

Public Function GetCity(strCountry As [String]) As String
    Dim strCity As String = String.Empty
    If strCountry = "USA" Then
        strCity = "NewYork"
    Else
        strCity = "OtherCity"
    End If
    Return strCity
End Function

我想從JavaScript懸停事件調用此函數。 我嘗試了以下方法,但不適用於帶parameter功能

 function popup(e) {
        var params = e.getAttribute('alt'); 
        var s = <%=GetCity() %>
    }

從JavaScript端調用時,如何將javascript參數傳遞給函數背后的代碼。

由於頁面方法無法在母版頁或用戶控件中使用,因此我無法使用PageMethods

請幫幫我。

您可以在javascript的隱藏字段中設置值。 創建回發按鈕具有css display:none服務器事件和調用回發按鈕的生成回發功能。 希望有幫助!

您可以從工具箱中創建一個簡單的頁面和拖放鏈接按鈕。 您可以在下面查看html源代碼

 <html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<script>

</script>

<body>
    <form method="post" action="Default2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTQ1OTQ0MTYyOWRkuWlHCHAXVuntHm4idSdrbGyB4bo1plZ25doIF2bKALo=" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKls9rrAgLM9PumD51kLQ9HHdzfMdVDeQCyvUBdqVdENTp5zu30+p/t+upk" />
</div>
    <a id="LinkButton1" href="javascript:__doPostBack(&#39;LinkButton1&#39;,&#39;&#39;)">LinkButton</a>
    </form>
</body>
</html>

您會看到它創建了函數__doPostBack。 而且鏈接不是回發的,因此會生成html到<a id="LinkButton1" href="javascript:__doPostBack(&#39;LinkButton1&#39;,&#39;&#39;)">LinkButton</a>因此,您可以創建此LinkBut​​ton具有所需的服務器事件,將鼠標懸停時隱藏鏈接css display:n中未設置js中隱藏字段的值,並使用隱藏的字段值調用js __doPostBack('LinkBut​​ton1','')到服務器回發過程。 我沒有顯示所有代碼。 你可以試試 。 其他方式,您可以ajax 在ajax后面的調用代碼后面調用代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM