tags:

views:

8

answers:

1

I have a aspx page with UpdatePanel. In code-behind, I define a public var and function. The function use input date to calc the public var from database.

 Public myRate As Decimal   
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    
 CalcT.Attributes.Add("onchange", "cstgst();")
  '......
   GetTaxRates()
 End Sub

 Public Function GetTaxRates() As Boolean
       //this function get user input date to calc the data from database
       Dim mydate As DateTime = UI.GetDate(DateInput.Text)  //UI.GetDate is a function to convert string to datetime
       '....
 End Function

In aspx page, I set the TextBox like:(AutoPostBack, inside UpdatePanel)

<asp:UpdatePanel>
 <asp:TextBox ID="DateInput" runat="server" AutoPostBack="true" ></asp:TextBox>
</asp:UpdatePanel>

then I try to get myRate in javascript like:

function Test(){
  var rate = '<%=myRate%>'
  //....
}

At first time to load this page, I can get the right value for myRate. But when I modified DateInput with new date value, the postback is fired and the function GetTaxRates() did was called and the right value was set in myRate when debugging the code and checking the value. Problem is javascript can't get this new value. It always get the first time value.

How to reslove this problem?

A: 

When you do a post-back from and updatePanel, only the item inside that updatePanel will be refreshed. If your javascript is outside of that updatePanel, it will not get the new value, it will always just have the value from the original page GET.

To resolve this, you need to get "myRate" inside the update panel so that it gets refreshed on every post-back from the panel. Either move your javascript inside the panel, or put a hidden control in the updatePanel and have your javascript read the value of that hidden control, like this: <asp:HiddenField ID="myRate" runat="server" Value="?" />

Greg
Thank you. How to move javescript function inside UpdatePanel?Like following way?<asp:UpdatePanel ID="EditUpdatePanel" RenderMode="Inline" runat="Server" UpdateMode="Conditional"> <ContentTemplate> <script type="text/javascript"> //.... </script> </ContentTemplate></asp:UpdatePanel>
KentZhou

related questions