0

How can I wire jQuery on a control that isn't rendered yet on the page? I tried overriding the following methods, but they didn't work as StepNavigationTemplateContainerID html markup is not yet available on this events: Render, OnLoad, OnInit, etc

Could it be because I'm using UpdatePanel? What method/event I could override when I'm using UpdatePanel so I can issue jQuery hooks there?

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    HookNextButton();
}       



void HookNextButton()
{
    string nextButtonClientId = wizardCooking.FindControl("StepNavigationTemplateContainerID").FindControl("btnNextStep").ClientID + "_lnkButton";
    var scriptKey = "Yo";

    string theScript =
        string.Format(
    @"
                    $(function() {{      


                        $('#{0}').click(function() {{
                            alert('hi');

                        }});

                    }});
                ", nextButtonClientId);
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, scriptKey))
    {
        ScriptManager.RegisterStartupScript(
                this, this.GetType(), scriptKey, theScript, true);
    }


}
Green Lantern
  • 858
  • 10
  • 21

1 Answers1

3

delegate it using .on() .. This will make sure the event is associated to the element which will be inserted into the DOM at a later time..

$('body').on('click' , '#buttonid' , function() {
    // Your code here
    // This will handle the case of buttons that 
    // will be rendered later

});
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • 'on' [doesn't work](http://jsfiddle.net/D7vMn/1/). 'live' [works](http://jsfiddle.net/D7vMn/2/) though. live has many [drawbacks](http://stackoverflow.com/questions/8042576/whats-the-difference-between-jquery-live-and-on) though. I'd rather prefer it happen during UpdatePanel's ajax. I'd upvote you though – Green Lantern Oct 16 '12 at 06:08
  • what is the version of jquery you are using.. If its version is older than 1.7 try using delegate() instead of on – Sushanth -- Oct 16 '12 at 06:10