简体   繁体   English

使用带有返回值的C#CodeBehind调用jQuery函数

[英]Calling jQuery function using C# CodeBehind with return value

I have an ASP.NET application that will be used to display information from a server regarding various sites for a water company. 我有一个ASP.NET应用程序,用于显示服务器中有关供水公司各种站点的信息。 I have a jQuery method that returns the text of the hyperlink which has been clicked within the div 'info': 我有一个jQuery方法,它返回在div'info'中单击的超链接的文本:

<script type="text/javascript">
        $('#info a').click(function getName()
        {
            return ($(this).text());
        });
</script>

I can call this method using C# codebehind using the code 我可以使用代码使用C#codebehind来调用此方法

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "getName()", true);

However I cannot get its return value, which is what I need. 但是我无法获得它的返回值,这就是我需要的。 Can anyone shed some light on this? 任何人都可以对此有所了解吗?

Use hidden field : 使用隐藏字段:

<input type="hidden" id="myhiddenField" name="myhiddenField" runat="server" />

And JQuery ( have not tested this ) : 和JQuery( 尚未测试过 ):

<script type="text/javascript">
        $('#info a').click(function getName()
        {
            $("#myhiddenField").val($(this).text());
        });
</script>

And then you would be able to access hidden field in code behind myhiddenField.Value . 然后,您将能够访问myhiddenField.Value后面的代码中的隐藏字段。

Or if you want to use Ajax Call see tutorial here 或者,如果您想使用Ajax Call,请参阅此处的教程

EDIT : 编辑:

I created a little project and the below works fine for me (I get alert "testing"): 我创建了一个小项目,以下工作对我来说很好(我得到警告“测试”):

 <script type="text/javascript">
        $(document).ready(function () {
            $('#info a').click(function getName() {
                // As control having runat="server" their ids get changed
                // selector would be like this 
                $("#<%= myhiddenField.ClientID %>").val($(this).text());
                alert($("#<%= myhiddenField.ClientID %>").val());
            });
        });
</script>

<div id="info">
  <a href="#">testing</a>
</div>
<input type="hidden" id="myhiddenField" name="myhiddenField" runat="server" />

You need to fire a button click event from JavaScript in ASP.NET after the document ready 在文档准备好之后,您需要从ASP.NET中的JavaScript激活按钮单击事件

like this 像这样

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "$(function() {
$( ‘#info a
‘ ).click(); });
", true);

for more details see Click() 有关详细信息,请参阅Click()

Try this Calling Javascript function on code behind 尝试在后面的代码上调用Javascript函数

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

And If you have UpdatePanel there then try like this 如果你有UpdatePanel,那么尝试这样

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

Updated Answer: 更新答案:

Client Side : Create a function and set value to hidden field as clientside , and on serverside call this function and get hiddenfield value 客户端:创建一个函数并将值设置为隐藏字段作为客户端,并在服务器端调用此函数并获取隐藏字段值

JS: JS:

  function myFunc(){
     //set you value to hiddenfield
     $(".hdfiled").val("Hello");
      alert($(".hdfiled").val());
    }

Code behind : Here am calling myFunc from serverside as your Title says call function from CODE BEHIND 代码背后:这里是从服务器端调用myFunc ,因为你的标题是来自CODE BEHIND的调用函数

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:myFunc(); ", true);
string getHd_Value=  myhiddenField.value;

JS FIDDLE TO check hiddenfield values JS FIDDLE来检查隐藏字段值

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

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