Thursday, March 8, 2012

Triggering an Update Panel from a control in a different ContentPlaceHolder

I can across an anamoly today when I tried to trigger an Update Panel from a control in a different ContentPlaceHolder.  The IDE recognized the control in the designer as I was able to select the control ID from the dropdown.  But, when I ran the page I received an error stating: "A control with ID 'Button1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'." 
This is something I think should be fixed in the AJAX framework.  The Unique ID of the button is "ctl00$ContentPlaceHolder2$Button1", but the UpdatePanel is in ContentPlaceHolder1 so it assigned the controlID of the trigger to "ctl00$ContentPlaceHolder1$Button1" causing the YSOD ("Yellow Screen of Death").
The work around that I currently use is to dynamically add the trigger to the update panel.

    protected void Page_Init()
    {
     
         //LinkButton lbtnStart = new LinkButton();
         //lbtnStart = (LinkButton)this.Form.FindControl("ContentPlaceHolder1").FindControl("lbtnStart");
        //AsyncPostBackTrigger tr = new AsyncPostBackTrigger();
        //tr.ControlID = lbtnStart.UniqueID;
        //UpdatePanel1.Triggers.Add(tr);

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.EventName = "Click";
        trigger.ControlID = lbtnStart.UniqueID;
        UpdatePanel1.Triggers.Add(trigger);
    }

และcomment ในUpdatePanel ตรง Triggers
  <asp:UpdatePanel ID="UpdatePanel1" runat="server"    >
        <ContentTemplate>
            <div runat="server" id="messageBox">
            </div>
        </ContentTemplate>
        <%--<Triggers>
           <asp:AsyncPostBackTrigger ControlID="lbtnStart" EventName="Click" />
        </Triggers>--%>
    </asp:UpdatePanel>

and 

No comments:

Post a Comment