Hcabnettek
Hcabnettek

Reputation: 12938

Can a userControl event cause an update panel to refresh?

I have an aspx.

 <div id="headerRegion" class="borderDiv">
    <xy:paymentHeader id="paymentHeader1" runat="server" />
</div>

<div id="paymentRegion" class="borderDiv">
    <asp:UpdatePanel ID="paymentFormUpdater" runat="server">
        <ContentTemplate>
            <asp:PlaceHolder runat="server" ID="plcPaymentForm" />
        </ContentTemplate>
    </asp:UpdatePanel>        
</div>

on page init, the placeHolder loads an ascx.

private Control GetPaymentControl(char? coverageBenefitPeriod)
    {
        Control paymentCtl = null;
        switch (coverageBenefitPeriod)
        {
            case 'L':
                paymentCtl = this.LoadControl("~/Controls/Lumpform.ascx");
                break;
            case 'W':
                paymentCtl = this.LoadControl("~/Controls/Periodicform.ascx");
                break;
            default:
                paymentCtl = this.LoadControl("~/Controls/Lumpform.ascx");
                break;
        }
        return paymentCtl;
    }

plcPaymentForm.Controls.Add(control);

There's a radioButton List on paymentHeader1 control. When I toggle that radio button would like to elegantly swap between Periodicform.ascx and Lumpform.ascx in the placeholder "plcPaymentForm". How do I do this correctly? I am trying not to load both controls and toggle their visibility. If you have any ideas how to do this properly with minimal page interuption please point me in the right direction.

Thanks, ~ck in San Diego

Upvotes: 2

Views: 9806

Answers (2)

vinay
vinay

Reputation: 56

Little different version of what drs9222 had answered.
1. Declare a delegate

Public delegate void UserControlFormSubmit(object sender, EventArgs e);

2. Declare an event inside user control of type UserControlFormSubmit

Public event UserControlFormSubmit OnFormSubmit;

3. Set User control event as trigger for update panel like this

<asp:UpdatePanel ID="paymentFormUpdater" runat="server" UpdateMode=”Conditional” ChildrenAsTriggers=”true”> 
<ContentTemplate> 
    <asp:PlaceHolder runat="server" ID="plcPaymentForm" /> 
</ContentTemplate> 
<Triggers>
   <asp:AsyncPostBackTrigger ControlID="paymentHeader1"            EventName="OnFormSubmit" />

4. Raise the event OnFormSubmit when selectedindexchange event occurs for the radioButtonList. (Note that you need to set AutoPostBack=true for radioButtonList as mentioned by drs9222.

Upvotes: 4

drs9222
drs9222

Reputation: 4508

I see three quick and dirty ideas:

  1. You could probably set the radio button list to autopostback and then bubble the event up so that xy:paymentHeader could be used as a trigger for the update panel.
  2. Have xy:paymentHeader raise an event and call the updatepanel's Update method in the event handler.
  3. Pass the updatepanel's id into the control and use find control to find the updatpanel and call its update method.

Example (for #1):


UserControl:

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = ((RadioButtonList)sender).SelectedValue;
    char? c = null;
    if (!string.IsNullOrEmpty(value))
    {
        c = value[0];
    }
    RaiseBubbleEvent(this, new CommandEventArgs("SelectedIndexChanged", c));
}

Page:

protected override bool OnBubbleEvent(object source, EventArgs args)
{
    if (args is CommandEventArgs)
    {
        CommandEventArgs cArgs = (CommandEventArgs)args;
        if (cArgs.CommandName == "SelectedIndexChanged")
        {
            Control c = GetPaymentControl((char?)cArgs.CommandArgument);
            // ...
            updatePanel.Update();
            return true;
        }
    }
    return base.OnBubbleEvent(source, args);
}

Upvotes: 1

Related Questions