1

I want to get a codebehind value in my javascript function. My scenario is, Lets say I have two Usercontrols A and B. I am setting a global value from UserControl A. When I come to UserControl B's page load, I am accessing that value and putting it into a hidden field. I have a button on UserControl B which calls a javascript function on client click. On that function I am trying to catch the hiddenfields current value. But I always get 0 from the hidden field value. What I am doing wrong? Can someone help me?

Below is the code:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="DetailsForm.ascx.vb" Inherits="TD_Web_App.DetailsForm" %>
<script type="text/javascript">
function GetNextRecord() {
    debugger;
    var currentVal = $get("DetailsFormPanel_DetailsForm1_HFDetails").value; 
//clientID of my hidden field
}
</script>
<asp:UpdatePanel ID="DetailInfoUpdatePanel" runat="server" UpdateMode="Conditional" > 

  <ContentTemplate>
<table id="TDPropsGrid" runat="server" width="250px" ></table>
    <asp:Button ID="Cmd_Next" runat="server" Text="Next" Width="100px" SkinID="ButtonSkin" UseSubmitBehavior="false" OnClientClick="GetNextRecord()"/>
<asp:LinkButton ID="LinkButtonDetails" runat="server" style="display:none;">
LinkButton 
</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>

<asp:HiddenField ID="HFDetails" runat="server" />

On code behind:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    showData() 'some function
    HFDetails.Value = CurrentRecordCounter  ' global value coming from other usercontrol 
End Sub

'Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
'    HFDetails.Value = CurrentRecordCounter
'End Sub

I have tried putting

<asp:HiddenField ID="HFDetails" runat="server" /> 

inside the update panel, nothing changed.

But at GetNextRecord() the currentVal is always "0". why?

3
  • Try var currentVal=$("#"DetailsFormPanel_DetailsForm1_HFDetails").val(); Commented Oct 3, 2012 at 8:36
  • Did you check the value of that hidden field in the HTML source in the browser? As in: is it the codebehind that's not setting the value or is it the javascript that's not reading it correctly? Commented Oct 3, 2012 at 9:01
  • thanks for the help guys. sorry for being late on this. dont know how, but the same code is working on another development environment. So all is well for now. I will look into this when I will have some spare time. Thanks again for your inputs. Commented Oct 5, 2012 at 8:01

1 Answer 1

2

Try:

var currentVal = $('#<%=HFDetails.ClientID %>').val();
Sign up to request clarification or add additional context in comments.

Comments

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.