简体   繁体   English

从带有参数的javascript调用函数后面的代码

[英]call code behind function from javascript with parameters

I have code Behind function in .ascx ( User Control ) as follows 我在.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

I want to call this function from JavaScript hover event. 我想从JavaScript悬停事件调用此函数。 I have tried the following method but it does not work for function with parameter 我尝试了以下方法,但不适用于带parameter功能

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

How to pass the javascript arguments to code behind function while it calling from JavaScript side. 从JavaScript端调用时,如何将javascript参数传递给函数背后的代码。

I am not able to use PageMethods since Page Methods don't work in either Master Pages or User Controls. 由于页面方法无法在母版页或用户控件中使用,因此我无法使用PageMethods .

Please help me out. 请帮帮我。

You can setting value in a hidden field in javascript. 您可以在javascript的隐藏字段中设置值。 Create post back button have css display:none have server event and call generate postback function of post back button. 创建回发按钮具有css display:none服务器事件和调用回发按钮的生成回发功能。 Hope this help! 希望有帮助!

You create a simple page and dragdrop link button from toolbox. 您可以从工具箱中创建一个简单的页面和拖放链接按钮。 you can view html source code below 您可以在下面查看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>

You see it create function __doPostBack . 您会看到它创建了函数__doPostBack。 And a link is not postback so it generate html to <a id="LinkButton1" href="javascript:__doPostBack(&#39;LinkButton1&#39;,&#39;&#39;)">LinkButton</a> so you can create this LinkButton have server event you need, hiden the link css display:none set value for hiden field in js when hover and call js __doPostBack('LinkButton1','') to server postback process with your hidden fiel value. 而且链接不是回发的,因此会生成html到<a id="LinkButton1" href="javascript:__doPostBack(&#39;LinkButton1&#39;,&#39;&#39;)">LinkButton</a>因此,您可以创建此LinkBut​​ton具有所需的服务器事件,将鼠标悬停时隐藏链接css display:n中未设置js中隐藏字段的值,并使用隐藏的字段值调用js __doPostBack('LinkBut​​ton1','')到服务器回发过程。 I don't show all code. 我没有显示所有代码。 You can try it . 你可以试试 。 Other way you can you ajax to call code behind call code behind ajax 其他方式,您可以ajax 在ajax后面的调用代码后面调用代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM