2010-04-26 129 views
4

我想寫一個報價生成器。對於每種產品,都有一組選項。我想爲每個選項動態添加一個下拉列表,然後讓它們的SelectedIndexChanged事件全部連線以更新報價成本。如何以編程方式將觸發器添加到ASP.NET UpdatePanel?

我沒有任何麻煩添加DropDownList控件到我的UpdatePanel,但我似乎無法連接事件。

頁面加載後,下拉列表中存在數據,但更改它們不會調用SelectedIndexChanged事件處理程序,QuoteUpdatePanel也不會更新。 我有這樣的事情:

編輯:由於programmatically adding AsyncPostBackTrigger controls is not supported,我有我的代碼更改,但我仍然沒有得到事件:

編輯2:嘗試添加一個佔位符到名單(而不是直接添加下拉到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(); 
} 
+0

當然!沒有輸入? – scottm 2010-04-26 23:04:02

回答

4

AFAIK,加入異步觸發對UpdatePanel控制編程工作。

解決方法是將它們添加在Page_Init事件,並觸發的ControlID屬性值控制唯一ID:

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

好像做這項工作。我在上面創建了類似的頁面/控件結構。所以有用戶控制QuotePropertyControlDefault頁面持有這個控制。

我添加了dropDownList.AutoPostBack = true屬性,並能夠從下拉列表中捕獲異步回發。所以,猜測問題出在這個屬性上。

還有一件事:如何註冊異步觸發器並不重要;兩種變體ScriptManager.RegisterAsyncPostBackControlAsyncPostBackTrigger都像魅力一樣工作(直到頁面的初始事件)。

這是我如何做的:

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

默認。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> 
+0

這是一個單獨的用戶控件。在當前的實現中,我在QuotePanelControl_Init()事件中添加了觸發器。在您的建議之後,我公開了PopulateUpdatePanel方法,並從Page的Init事件中調用它。我仍然沒有得到任何事件。 – scottm 2010-04-27 18:28:58

+0

@scottm:我會在我身邊嘗試並報告進度 – Alex 2010-04-27 20:15:25

+0

這就是問題所在!謝謝 – scottm 2010-04-27 21:54:56

相關問題