简体   繁体   English

从转发器内部的控件创建asyncpostback

[英]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: 我有一个转发器,该转发器的ItemTemplate包含一个PlaceHolder,我将输入控件(ListBox,TextBox,CalendarExtender等)添加到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)? 如何从中继器中的这些TextBox之一(在TextChanged上)生成asyncpostback?

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) OnItemCommand(但这似乎仅用于按钮)
  2. ScriptManager.RegisterAsyncPostBackControl (doesn't seem to do anything on TextChanged) ScriptManager.RegisterAsyncPostBackControl(似乎对TextChanged不做任何事情)
  3. UpdatePanel.Triggers.Add(new AsyncPostBackTrigger ...) (unable to find the TextBox as it is inside the repeater) UpdatePanel.Triggers.Add(new AsyncPostBackTrigger ...)(无法找到文本框,因为它位于转发器内部)

In ReportParameterEditRepeater_ItemDataBound you will need to assign a unique ID to each control, and then bind the text changed event. 在ReportParameterEditRepeater_ItemDataBound中,您需要为每个控件分配一个唯一的ID,然后绑定文本更改事件。 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): 然后,在页面的OnInit中,由于要动态创建它们,因此必须重新绑定viewstate的所有内容,这是您创建的唯一ID出现的位置(主要用于回发):

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM