6

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?

8
  • 1
    You cannot call client side script and get return values in server side code. If you want your server code to react to a link being clicked then there are options (submit the form with a hidden field or use an ajax call, for example), but what you've got above simply won't work. I'd strongly recommend that you do some reading about the ASP.Net Page Life Cycle; Commented Oct 1, 2013 at 9:01
  • the question is where you want to set those values , just update the value of that control ? why is return required? Commented Oct 1, 2013 at 9:01
  • I agree with @Archer, use ajax call or hidden field Commented Oct 1, 2013 at 9:02
  • or embedded code behind C# <% SeverSideProperties %> ? Commented Oct 1, 2013 at 9:04
  • @RameezAhmedSayad He's trying to return the value of a client-side script to a server function. Commented Oct 1, 2013 at 9:07

3 Answers 3

7

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="#">testing</a>
</div>
<input type="hidden" id="myhiddenField" name="myhiddenField" runat="server" />
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks. What C# code will I need to access this hidden field? Sorry I am fairly new to ASP
You can use it like any other field try myhiddenField.Value - updated answer
I am not able to access myHiddenField (does not exist in current context)
oops forgot runat="server" in hidden field. try that please :)
Everything runs smoothly but myHiddenField.Value comes back without a value, is it null for some reason?
|
1

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

Comments

-1

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

2 Comments

These work for calling the function however I cannot get the function's return value - is there any additional code I can use to grab the return value?
He's trying to return the value of a client-side script to a server function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.