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. I have a jQuery method that returns the text of the hyperlink which has been clicked within the 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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use hidden field :

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

And JQuery (have not tested this) :

<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.

Or if you want to use Ajax Call see tutorial here

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="#" rel="nofollow noreferrer noopener">testing</a>
</div>
<input type="hidden" id="myhiddenField" name="myhiddenField" runat="server" />

Method 2

You need to fire a button click event from JavaScript in ASP.NET after the document ready

like this

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

for more details see Click()

Method 3

Try this
Calling Javascript function on code behind

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

And If you have UpdatePanel there then try like this

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:

  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

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

JS FIDDLE TO check hiddenfield values


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x