RegisterStartupScript in User Control does not reload/ rewrite Javascript

1.7k Views Asked by At

UPDATE

Summary of the problem:

  1. There are 6 Dynamic UCs which are generated and each has its own custom JS which is injected using RegisterStartUpScript on Page Init of UC

  2. Only the 1st time the Parent Page which generates the 1st UC will have the script correctly placed at the bottom of the page.

  3. On subsequent Partial Updates ( AJAX call ) to generate the new UC, the associated script is not injected...

I'll continue searching and try different things, but if anyone can guide me, it 'll be greatly appreciated Thanks!

Below is the full explanation

I have a Parent page using a Master in which the RadScriptManager is defined ( telerik's Script manager). The Parent Page has a placeholder where a dynamic User Control is generated . The UC has a javascript which I inject on Page_Init of UC.

When I call it the first time, 1st page load, the script works correctly.. I can see it on the source code in Chrome debugger. When I change a ddl on the Parent, it 's call and generate another UC within the placeholder. The latter has its own Javascript Inject similar to the first one generated... both UC are basically Views for each province so it has the same structure but the form content are slightly different...

The problem is, when I view the Chrome Debugger i see the partial update of the content of the User COntrol, but i do not see the new script from the 2nd control which was supposed to be added to add_load on Page_Init of the UC. So Everytime , I change control, it calls the previous script... although my code goes through the section below... which is basically registering the appropriate script for the given control being loaded.

In the example below, I always see an alert "SK cmbSearch = .." as opposed to "ON cmbSearch = ..." which is the current province i was trying to load.

Can anyone give me some pointers?

public void RegisterSearchVisibilityPPSA()
{
string jsFunction =
@"function DefineView(sender, eventArgs) {
    var comboSearch = $find('%%cmbSearchType%%');
    //cmbSearch Section
    alert('ON cmbSearch = %%cmbSearchType%% with value = ' + comboSearch.get_selectedItem().get_value() );
    switch (comboSearch.get_selectedItem().get_value()) {
        //PER 
        case 'IN':
        case 'IS' :
            $('#tblPersonSearchForm').show();
            $('#tblPersonDOBSearchForm').show();
            $('#tblBusinessSearchForm').hide();
            $('#tblSerialNumberedSearchForm').hide();
            break;
        //BUS 
        case 'BD':
            $('#tblPersonSearchForm').hide();
            $('#tblPersonDOBSearchForm').hide();
            $('#tblBusinessSearchForm').show();
            $('#tblSerialNumberedSearchForm').hide();
            break;
        //SER 
        case 'MVE':
        case 'MVS':
            $('#tblPersonSearchForm').hide();
            $('#tblPersonDOBSearchForm').hide();
            $('#tblBusinessSearchForm').hide();
            $('#tblSerialNumberedSearchForm').show();
            break;
        //REG 
        case 'REG':
        default:
            $('#tblPersonSearchForm').hide();
            $('#tblPersonDOBSearchForm').hide();
            $('#tblBusinessSearchForm').hide();
            $('#tblSerialNumberedSearchForm').hide();
            $('#tblRegistrationNumberedSearchForm').show();
            break;
    }


}

function onPageLoad(){
    // jquery Area
    $(document).ready(function () {
        DefineView();
    });
}
";


RadScriptManager.RegisterStartupScript(this, this.GetType(), "ON_SearchVisibilityPPSA",jsFunction.Replace("%%cmbSearchType%%", cmbSearchType.ClientID), cmbRegistrationType.ClientID), true);
string onLoadScript = @" Sys.Application.add_load(function(){ onPageLoad();}) ";
RadScriptManager.RegisterStartupScript(this, this.GetType(), "ON_onLoadScript", onLoadScript, true);
}
1

There are 1 best solutions below

0
On

Upon an Ajax request I think you should use the RadAjaxPanel.ResponseScripts.Add(script).