且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何以编程方式添加触发器一个ASP.NET的UpdatePanel?

更新时间:2023-11-30 15:34:58

据我所知,增加异步触发对的UpdatePanel 控制编程工作。

解决方法是将其添加在 Page_Init 事件,并触发的控件ID 属性值控制的唯一ID:

  AsyncPostBackTrigger触发=新AsyncPostBackTrigger();
//唯一的ID,而不是客户端ID
trigger.ControlID = yourDropDownControl.UniqueID;
trigger.EventName =的SelectedIndexChanged;
QuoteUpdatePanel.Triggers.Add(触发);

秒>
好像做这项工作。我上面创建页/控制的类似的结构。因此,有用户控件 QuotePropertyControl 默认页,其中包含此控件。

我添加 dropDownList.AutoPostBack = TRUE 属性,并能够从下拉菜单中捕获一个异步回发。因此,猜测的问题是在这个属性。

一件事:它真的不要紧如何注册异步触发;这两个变种 ScriptManager.RegisterAsyncPostBackControl 并通过 AsyncPostBackTrigger 工作就像一个魅力(直到页面的init事件)。

下面是我做的:

QuotePropertyControl.ascx.cs

 私人的String []数据= {A,B,C,D,E};公共无效PopluateUpdatePanel(IEnumerable的<串GT; standardOptions)
{
    的foreach(在standardOptions串standardOp)
    {
        DropDownList的DROPDOWNLIST =新的DropDownList();
        dropDownList.SelectedIndexChanged + =
            QuotePropertyDropDown_SelectedIndexChanged;
        dropDownList.ID = standardOp +的DropDownList
        dropDownList.CssClass =报价-财产的DropDownList
        dropDownList.AutoPostBack = TRUE;
        dropDownList.DataSource =数据;
        dropDownList.DataBind();        标签propertyLabel =新标签(){文字= standardOp};        StandardOptionsPlaceHolder.Controls.Add(propertyLabel);
        StandardOptionsPlaceHolder.Controls.Add(DROPDOWNLIST);        ScriptManager.GetCurrent(页)
            .RegisterAsyncPostBackControl(DROPDOWNLIST);
    }
}保护无效QuotePropertyDropDown_SelectedIndexChanged(
    对象发件人,
    EventArgs的
    )
{
    StandardOptionsUpdatePanel.Update();
}

QuotePropertyControl.ascx

 < ASP:的UpdatePanel ID =QuoteUpdatePanel=服务器ChildrenAsTriggers =真>
    <&的ContentTemplate GT;
        成本:
        < ASP:标签ID =QuoteCostLabel=服务器/>
        <字段集ID =标准选项>
            <传奇>标准选项< /传说>
            < ASP:的UpdatePanel ID =StandardOptionsUpdatePanel
                =服务器
                ChildrenAsTriggers =真
                的UpdateMode =条件>
                <&的ContentTemplate GT;
                    < ASP:占位符ID =StandardOptionsPlaceHolder
                    =服务器/>
                < /&的ContentTemplate GT;
            < / ASP:的UpdatePanel>
        < /字段集>
    < /&的ContentTemplate GT;
< / ASP:的UpdatePanel>

Default.aspx.cs

 的String [] =名称{AB,BC,EF};保护无效Page_Init(对象发件人,EventArgs的发送)
{
    ctlQuoteProperty.PopluateUpdatePanel(地名);
}

Default.aspx的

 <%@注册SRC =〜/ QuotePropertyControl.ascx
             标签preFIX =UC
             标签名=QuoteProperty%GT;<表ID =form1的=服务器>
< D​​IV>
    < ASP:的ScriptManager ID =ScriptManager的=服务器/>
    < UC:QuoteProperty =服务器
        ID =ctlQuoteProperty>
    < / UC:QuoteProperty>
< / DIV>
< /表及GT;

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:

Edit: Since programmatically adding AsyncPostBackTrigger controls is not supported, I've change my code to this, but I still don't get the event:

Edit 2: Tried adding a PlaceHolder to add the drop down lists to (instead directly into the ContentTemplateContainer, still no events firing.

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>
                <asp:PlaceHolder ID="StandardOptionsPlaceHolder" runat="server" />                   
                </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(IEnumerable<IQuoteProperty> standardOptions)
{
    foreach (IQuoteProperty standardOp in standardOptions)
    {
        QuotePropertyDropDownList<IQuoteProperty> dropDownList = new QuotePropertyDropDownList<IQuoteProperty>(standardOp);
        dropDownList.SelectedIndexChanged += QuotePropertyDropDown_SelectedIndexChanged;
        dropDownList.ID = standardOp.GetType().Name + "DropDownList";
        dropDownList.CssClass = "quote-property-dropdownlist";

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

        StandardOptionsPlaceHolder.Controls.Add(propertyLabel);
        StandardOptionsPlaceHolder.Controls.Add(dropDownList);

        _standardOptionsDropDownLists.Add(dropDownList);

        ScriptManager.RegisterAsyncPostBackControl(dropDownList);

    }

}

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

AFAIK, adding async triggers to the UpdatePanel control programmatically is working.

The workaround is to add them in Page_Init event and set trigger's ControlID property to the control unique id value:

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
// unique id instead of client id
trigger.ControlID = yourDropDownControl.UniqueID;  
trigger.EventName = "SelectedIndexChanged"; 
QuoteUpdatePanel.Triggers.Add(trigger);

Seems made this work. I created similar structure of page/control above. So there are user control QuotePropertyControl and Default page which holds this control.

I added dropDownList.AutoPostBack = true property and was able to catch an async postback from the dropdown. So, guessing the problem was in this property.

One more thing: it's really doesn't matter how to register async trigger; both variants ScriptManager.RegisterAsyncPostBackControl and through AsyncPostBackTrigger worked like a charm (until the init event of the page).

Here is how I did it:

QuotePropertyControl.ascx.cs

private string[] data = { "a", "b", "c", "d", "e" };

public void PopluateUpdatePanel(IEnumerable<string> standardOptions)
{
    foreach (string standardOp in standardOptions)
    {
        DropDownList dropDownList = new DropDownList();
        dropDownList.SelectedIndexChanged +=
            QuotePropertyDropDown_SelectedIndexChanged;
        dropDownList.ID = standardOp + "DropDownList";
        dropDownList.CssClass = "quote-property-dropdownlist";
        dropDownList.AutoPostBack = true;
        dropDownList.DataSource = data;
        dropDownList.DataBind();

        Label propertyLabel = new Label() { Text = standardOp };

        StandardOptionsPlaceHolder.Controls.Add(propertyLabel);
        StandardOptionsPlaceHolder.Controls.Add(dropDownList);

        ScriptManager.GetCurrent(Page)
            .RegisterAsyncPostBackControl(dropDownList);
    }
}

protected void QuotePropertyDropDown_SelectedIndexChanged(
    object sender,
    EventArgs e
    )
{
    StandardOptionsUpdatePanel.Update();
}

QuotePropertyControl.ascx

<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>
                    <asp:PlaceHolder ID="StandardOptionsPlaceHolder" 
                    runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>

Default.aspx.cs

string[] names = { "ab", "bc", "ef" };

protected void Page_Init(object sender, EventArgs e)
{
    ctlQuoteProperty.PopluateUpdatePanel(names);
}

Default.aspx

<%@ Register Src="~/QuotePropertyControl.ascx" 
             TagPrefix="uc" 
             TagName="QuoteProperty" %>

<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager" runat="server" />
    <uc:QuoteProperty runat="server"
        ID="ctlQuoteProperty">
    </uc:QuoteProperty>
</div>
</form>