I have the following code -
protected string Term
{
get { return this.ViewState["Term"] != null ? (string)this.ViewState["Term"] : ""; }
set { this.ViewState["Term"] = value; }
}
Then set Term
-
public void populateTerm()
{
Term = "Test";
ScriptManager.RegisterStartupScript(this, GetType(), "term","useTerm()", true);
}
Then in Javascript can use Term -
function useTerm() {
var Term = <%= Term %>;
// use Term
}
This works fine when the page is getting reloaded on the button click that was firing populateTerm()
. However I have since moved the button into an updatePanel
, so because the entire page is not getting reloaded on button click (only the update panel), this means that useTerm()
is not called and <%= Term %>
is null.
If I remove the updatePanel code it works fine. How can I get this to work with the updatePanel?
Assuming that the function
useTerm
is in you aspx page, you can move that script block within theUpdatePanel
ContentTemplate
.An alternative would be to change the JavaScript function so term is passed in as a parameter:
and then to call it with the latest value of
Term
: