How can I programmatically add triggers to an ASP.NET UpdatePanel?

Posted by scottm on Stack Overflow See other posts from Stack Overflow or by scottm
Published on 2010-04-26T17:50:51Z Indexed on 2010/04/26 17:53 UTC
Read the original article Hit count: 240

Filed under:
|

I am trying to write a quote generator. For each product, there are a set of options. I want to dynamically add a drop down list for each option, and then have their SelectedIndexChanged events all wired up to update the quote cost.

I am not having any trouble adding the DropDownList controls to my UpdatePanel, but I can't seem to wire up the events.

After the page loads, the drop downs are there, with their data, but changing them does not call the SelectedIndexChanged event handler, nor does the QuoteUpdatePanel update. I have something like this:

QuotePanel.ASCX

<asp:ScriptManager ID="ScriptManager" runat="server" />

<asp:UpdatePanel ID="QuoteUpdatePanel" runat="server" ChildrenAsTriggers="true">
    <ContentTemplate>
        Cost: <asp:Label ID="QuoteCostLabel" runat="server" />
        <fieldset id="standard-options">
            <legend>Standard Options</legend>
            <asp:UpdatePanel ID="StandardOptionsUpdatePanel" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
                <ContentTemplate>                   
                </ContentTemplate>
            </asp:UpdatePanel>
        </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>

The code to add the dropdowns and the event they are to be wire up for:

protected void PopluateUpdatePanel(IQuoteProperty standardOptions)
    foreach (IQuoteProperty standardOp in standardOptions)
    {
        QuotePropertyDropDownList<IQuoteProperty> dropDownList = new QuotePropertyDropDownList<IQuoteProperty>(standardOp);
        dropDownList.SelectedIndexChanged += new EventHandler(QuotePropertyDropDown_SelectedIndexChanged);
        dropDownList.ID = standardOp.GetType().Name + "DropDownList";

        ScriptManager.RegisterAsyncPostBackControl(dropDownList);

        Label propertyLabel = new Label() {Text = standardOp.Title, CssClass = "quote-property-label"};

        this.StandardOptionsUpdatePanel.ContentTemplateContainer.Controls.Add(propertyLabel);
        this.StandardOptionsUpdatePanel.ContentTemplateContainer.Controls.Add(dropDownList);
        _standardOptionsListBoxes.Add(dropDownList);

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger()
                                           {
                                               ControlID = dropDownList.UniqueID,
                                               EventName = "SelectedIndexChanged"
                                           };
        this.StandardOptionsUpdatePanel.Triggers.Add(trigger);
    }
}

void QuotePropertyDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    QuoteCostLabel.Text = QuoteCost.ToString();
}

© Stack Overflow or respective owner

Related posts about asp.net-ajax

Related posts about updatepanel