Can't get server side var value after postback in UpdatePanel
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 开发者_Go百科= 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?
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="?" />
精彩评论