I have an ASP.NET application that will be used to display information from a server regarding various sites for a water pany. 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?
I have an ASP.NET application that will be used to display information from a server regarding various sites for a water pany. 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?
Share Improve this question edited Jul 15, 2014 at 15:20 Phil asked Oct 1, 2013 at 8:53 PhilPhil 5834 gold badges6 silver badges17 bronze badges 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 remend that you do some reading about the ASP.Net Page Life Cycle; – Reinstate Monica Cellio 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? – Rameez Ahmed Sayad Commented Oct 1, 2013 at 9:01
- I agree with @Archer, use ajax call or hidden field – Zaki Commented Oct 1, 2013 at 9:02
-
or embedded code behind C#
<% SeverSideProperties %>
? – Rameez Ahmed Sayad Commented Oct 1, 2013 at 9:04 -
@RameezAhmedSayad He's trying to
return
the value of a client-side script to a server function. – Reinstate Monica Cellio Commented Oct 1, 2013 at 9:07
3 Answers
Reset to default 7Use 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" />
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()
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