简体   繁体   中英

Call JavaScript function after Telerik RadPageView finishes loading?

I'm using Telerik UI for asp.net. Specifically I'm using RadTabStrip with partial page postbacks to allow the user to tab through different sets of data. When the user clicks a tab, some code executes and loads data just for that tab.

I've figured out how to execute codebehind: I set the OnTabClick property of the RadTabStrip, and then in codebehind I check what tab was clicked.

Eg

protected void tab_Click(object sender, RadTabStripEventArgs e)
        {
            if (e.Tab.Text == "Info")
            {
                populateInfoTab();
            }
        }

private void populateInfotab()
{
    // Do some stuff
}

However, I can't figure out how to execute client side javascript after a specific tab is clicked. What I tried:
Set OnClientTabSelected property, and then add some javascript:

function tab_ClientClick(sender, args)
{
    var tab = args.get_tab();
    if(tab.get_text() == "Info")
    {
        alert("Tab Clicked");
    }
}

The problem is that I need to set the InnerHtml of some div in the clicked pageview after it is clicked. The div does not exist on page load (that specific RadPageView is hidden) so I cannot set it then. Once the user clicks into the tab, and after the page view loads, I need to be able to update the div's InnerHtml through JavaScript.

How would I go about doing this?

First option - if you do not set the RenderSelectedPageOnly property to true , all page views will be rendered on the initial load and you will be able to use JS to find/modify elements in them.

Second option - just set the content from the server as soon as you load the UC, this will usually make things simpler.

Third option - use client-side events (offered by the native PageRequestManager class or the RadAjaxManager, depending on how you setup your AJAX interactions) to execute when the response is received. The difficulty here is to determine which is the postback you need. Looking for the desired element and only executing logic if it exists is the simplest flag you can opt for.

Fourth option - register a script from the server code that will call your function, something like:

populateInfoTab();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someKey", "myDesiredFunction()", true);

where you may want to use the Sys.Application.Load to ensure it is executed later than IScriptControl initialization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM