加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

如何以编程方式将触发器添加到ASP.NET UpdatePanel?

发布时间:2020-12-16 09:57:49 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试编写一个引用生成器.对于每种产品,都有一组选项.我想为每个选项动态添加一个下拉列表,然后将他们的SelectedIndexChanged事件全部连接起来以更新报价成本. 我将DropDownList控件添加到我的UpdatePanel没有任何问题,但我似乎无法连接事件. 页面加载
我正在尝试编写一个引用生成器.对于每种产品,都有一组选项.我想为每个选项动态添加一个下拉列表,然后将他们的SelectedIndexChanged事件全部连接起来以更新报价成本.

我将DropDownList控件添加到我的UpdatePanel没有任何问题,但我似乎无法连接事件.

页面加载后,下拉列表及其数据,但更改它们不会调用SelectedIndexChanged事件处理程序,QuoteUpdatePanel也不会更新.
我有这样的事情:

编辑:自programmatically adding AsyncPostBackTrigger controls is not supported以来,我已将我的代码更改为此,但我仍然没有得到该事件:

编辑2:尝试添加PlaceHolder以将下拉列表添加到(而不是直接进入ContentTemplateContainer,仍然没有触发事件.

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>

添加下拉列表的代码以及它们要连接的事件:

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,以编程方式向UpdatePanel控件添加异步触发器正在工作.

解决方法是在Page_Init事件中添加它们,并将trigger的ControlID属性设置为控件唯一ID值:

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

似乎使这项工作.我在上面创建了类似的页面/控件结构.因此有用户控制QuotePropertyControl和默认页面,它持有此控件.

我添加了dropDownList.AutoPostBack = true属性,并能够从下拉列表中捕获异步回发.所以,猜测问题出在这个属性中.

还有一件事:如何注册异步触发器并不重要;两种变体ScriptManager.RegisterAsyncPostBackControl和AsyncPostBackTrigger都像魅力一样工作(直到页面的init事件).

我是这样做的:

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读