ScriptManager run script immediately

2019-05-14 01:08发布

问题:

I have an update panel load a user control, and the goal here is to have a specific JavaScript function called when this occurred. I was thinking this would be done through the Script Manager, though this does not need to be the case if there's an html alternative.

The flow is basically the following:

  • User clicks button
  • Update panel loads control
  • Javascript function is called

Many thanks

I've already tried RegisterStartupScript(..), though i may have made a syntactical mistake

if (show)
{ 
    string scriptId = String.Format("{0}:script", ++uniquePopupId);
    string scriptTag = String.Format("showPopup({0},{1});", Width, Height);
    ScriptManager.RegisterStartupScript(this, this.GetType(), scriptId, scriptTag, true);
}   

Solution:

if (show)
{ 
    string scriptId = String.Format("{0}:script", ++uniquePopupId);
    string scriptTag = String.Format("showPopup({0},{1});", Width, Height);
    ScriptManager.RegisterStartupScript(updPanelChildControl, updPanelChildControl.GetType(), scriptId, scriptTag, true);
}   

回答1:

Use

ScriptManager.RegisterStartupScript(myControlInstance, 
                                    typeof(MyControl),
                                     "key",
                                     "my javascript string here", 
                                     true);

Documentation here.