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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19111890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:07:46  来源:igfitidea点击:

Calling jQuery function using C# CodeBehind with return value

c#javascriptjqueryasp.net

提问by Phil

I have an ASP.NET application that will be used to display information from a server regarding various sites for a water company. I have a jQuery method that returns the text of the hyperlink which has been clicked within the div 'info':

我有一个 ASP.NET 应用程序,它将用于显示来自服务器的有关自来水公司各个站点的信息。我有一个 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# 代码隐藏调用此方法

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?

但是我无法获得它的返回值,这正是我所需要的。任何人都可以对此有所了解吗?

采纳答案by Zaki

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" />

回答by Satinder singh

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 myFuncfrom serverside as your Title says call function from CODE BEHIND

后面的代码:这里是myFunc从服务器端调用,因为您的标题表示来自代码后面的调用函数

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

JS FIDDLE TO check hiddenfield values

JS FIDDLE 检查隐藏字段值

回答by Sain Pradeep

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()