Chris B
Chris B

Reputation: 709

Creating an asyncpostback from a control inside a repeater

I have a repeater whose ItemTemplate contains a PlaceHolder that I add input controls (ListBox, TextBox, CalendarExtender etc.) to on ItemDataBound:

<asp:UpdatePanel ID="ReportParameterUpdatePanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:Repeater ID="ReportParameterEditRepeater" OnItemDataBound="ReportParameterEditRepeater_ItemDataBound" runat="server">
            <ItemTemplate>
                <asp:PlaceHolder runat="server" ID="ParameterEntryPlaceholder"></asp:PlaceHolder>
            </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>

How can I generate an asyncpostback from one of these TextBoxes inside the repeater (on TextChanged)?

The control is created dynamically and I only want to create the postback under certain conditions, so it needs to be done from code behind.

I have tried:

  1. OnItemCommand (but this appears to be just for buttons)
  2. ScriptManager.RegisterAsyncPostBackControl (doesn't seem to do anything on TextChanged)
  3. UpdatePanel.Triggers.Add(new AsyncPostBackTrigger ...) (unable to find the TextBox as it is inside the repeater)

Upvotes: 0

Views: 856

Answers (1)

Steve Stokes
Steve Stokes

Reputation: 1230

In ReportParameterEditRepeater_ItemDataBound you will need to assign a unique ID to each control, and then bind the text changed event. I then like to store those in session. Then add it to your placeholder. Below is how I did it in my site for a button click event:

TemplateControls_Headline ctrl = (TemplateControls_Headline)LoadControl("~/Controls/TemplateHeadline.ascx");
ctrl.ID = "MyCtrl_" + CMSSession.Current.AddedTemplateControls.Count;
ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);

MySession.Current.AddedTemplateControls.Add((Control)ctrl);

PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;
ph.Controls.Add(ctrl);

Then, in your OnInit of the page, you have to re-bind everything from the viewstate since you are creating them dynamically, this is where the unique id you created comes in (this is mainly for postbacks):

protected override void OnInit(EventArgs e)
{
    PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;

    int counter = 0;

    foreach (UserControl ctrl in MySession.Current.AddedTemplateControls)
    {
        ctrl.ID = "MyCtrl_" + counter;
        ctrl.Remove.CommandArgument = counter.ToString();
        ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);
        counter++;
        ph.Controls.Add(ctrl);
    }

    base.OnInit(e);
}

Upvotes: 1

Related Questions