我如何以编程方式添加触发器一个ASP.NET的UpdatePanel? [英] How can I programmatically add triggers to an ASP.NET UpdatePanel?

查看:114
本文介绍了我如何以编程方式添加触发器一个ASP.NET的UpdatePanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写报价发生器。对于每个产品,有一组选项。我想动态添加一个下拉列表中为每个选项,然后有自己的SelectedIndexChanged事件的所有接线更新报价成本。

我没有添加DropDownList控件到我的UpdatePanel任何麻烦,但我似乎无法要连接的事件。

在页面加载后,下拉菜单都在那里,他们的数据,但改变它们不调用SelectedIndexChanged事件处理程序,也没有了QuoteUpdatePanel更新。
我有这样的事情:

编辑:由于编程加入AsyncPostBackTrigger控制不支持,我已经改变我的code到这一点,但我仍然没有得到事件:

编辑2:尝试添加一个占位符添加下拉列表来(而不是直接进入ContentTemplateContainer,仍然没有事件触发

QuotePanel.ASCX

 < ASP:的ScriptManager ID =ScriptManager的=服务器/>< 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>

在code以添加下拉式菜单中,他们成为了线的事件:

 保护无效PopluateUpdatePanel(IEnumerable的< IQuoteProperty> standardOptions)
{
    的foreach(IQuoteProperty standardOp在standardOptions)
    {
        QuotePropertyDropDownList< IQuoteProperty> DROPDOWNLIST =新QuotePropertyDropDownList< IQuoteProperty>(standardOp);
        dropDownList.SelectedIndexChanged + = QuotePropertyDropDown_SelectedIndexChanged;
        dropDownList.ID = standardOp.GetType()名称+DropDownList的。
        dropDownList.CssClass =报价-财产的DropDownList        标签propertyLabel =新标签(){文字= standardOp.Title,的CssClass =引用属性标签};        StandardOptionsPlaceHolder.Controls.Add(propertyLabel);
        StandardOptionsPlaceHolder.Controls.Add(DROPDOWNLIST);        _standardOptionsDropDownLists.Add(DROPDOWNLIST);        ScriptManager.RegisterAsyncPostBackControl(DROPDOWNLIST);    }}无效QuotePropertyDropDown_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    QuoteCostLabel.Text = QuoteCost.ToString();
    StandardOptionsUpdatePanel.Update();
}


解决方案

据我所知,增加异步触发对的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>

这篇关于我如何以编程方式添加触发器一个ASP.NET的UpdatePanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆