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?
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" />
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With