Question

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?

Was it helpful?

Solution

Assuming that the function useTerm is in you aspx page, you can move that script block within the UpdatePanel ContentTemplate.

An alternative would be to change the JavaScript function so term is passed in as a parameter:

function useTerm(term) {
    // use term
}

and then to call it with the latest value of Term:

public void populateTerm()
{

    Term = "Test";    
    string script = string.Format("useTerm('{0}');", Term);
    ScriptManager.RegisterStartupScript(this, GetType(), "term", script, true);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top