2011-03-03 56 views
1

我發現了很多解決方案,但沒有一個在我的方案中不起作用。 我已經創建了一個測試項目來演示我的概念。updatepanel和javascript include文件

基本上,有承載用戶控件的頁面...

<body> 
<form id="form1" runat="server"> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<div> 
    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" /> 
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
    <br /> 
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
</div> 
</form> 

WebUserControl1具有下拉列表和另外兩個webusercontrols(基於下拉列表元素的選擇要被顯示)的內部更新面板如下。隱藏文件WebUserControl1是

<%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %> 
<%@ Register Src="WebUserControl3.ascx" TagName="WebUserControl3" TagPrefix="uc3" %> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 
    <asp:DropDownList ID="DropDownList1" runat="server"     
      OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 
     AutoPostBack="True"> 
    </asp:DropDownList> 
    <asp:Panel ID="pnlCreditCard" Visible="false" runat="server"> 
     <uc2:WebUserControl2 ID="WebUserControl21" runat="server" /> 
    </asp:Panel> 
    <asp:Panel ID="pnlGiftCard" Visible="false" runat="server"> 
     <uc3:WebUserControl3 ID="WebUserControl31" runat="server" /> 
    </asp:Panel> 
</ContentTemplate> 
</asp:UpdatePanel> 

代碼.....

 public enum PaymentMethod 
    { 
     CreditCard = 0, 
     GiftCard 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
      BindPaymentMethods(Enum.GetValues(typeof(PaymentMethod))); 
    } 

    private void BindPaymentMethods(Array paymentMethods) 
    { 
     DropDownList1.DataSource = paymentMethods; 
     DropDownList1.DataBind(); 

     if (paymentMethods.Length > 0) 
     { 
      DropDownList1.SelectedIndex = 0; 
      UpdateCreditOrGiftCardPanelVisibility(); 
     } 
    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     UpdateCreditOrGiftCardPanelVisibility(); 
    } 

    private void UpdateCreditOrGiftCardPanelVisibility() 
    { 
     if(DropDownList1.SelectedValue == Enum.GetName(typeof(PaymentMethod),PaymentMethod.CreditCard)) 
     { 
      pnlGiftCard.Visible = false; 
      pnlCreditCard.Visible = true; 
     } 
     else if (DropDownList1.SelectedValue == Enum.GetName(typeof(PaymentMethod), PaymentMethod.GiftCard)) 
     { 
      pnlCreditCard.Visible = false; 
      pnlGiftCard.Visible = true; 
     } 
    } 

現在,問題就在這裏開始...有一個外部JavaScript文件[JScript1.js](內嵌資源),這基本上是用來顯示一個警告框。

<script language="javascript" type="text/javascript"> 

window.onload = function() { 
    alert('creditcard form'); 
} 

WebUserControl2.ascx.cs代碼背後,是當我改變下拉列表的值沒有顯示

 protected void Page_Load(object sender, EventArgs e) 
    { 
     ScriptManager.RegisterClientScriptInclude(this.Page, this.Page.GetType().BaseType, "JScript1", Page.ClientScript.GetWebResourceUrl(this.Page.GetType().BaseType, "WebApplication1.JScript1.js")); 
    } 

警報窗口。即使腳本也被註冊了三次(看看firebug) 由於原始JS文件太大,需要使用ScriptInclude而不是ScriptBlock。

可以通過電子郵件的測試程序....

由於提前

回答

0

了一下週圍的工作後,我找到了解決辦法。

我WebUserControl2.ascx

<asp:ScriptManagerProxy ID="ScriptManager1" runat="server" > 
<Scripts> 
    <asp:ScriptReference Assembly="WebApplication1" Name="WebApplication1.JScript1.js" /> 
</Scripts> 
</asp:ScriptManagerProxy> 

註冊的ScriptManagerProxy然後在同一控制背後的代碼,添加...

 protected override void OnPreRender(EventArgs e) 
    { 
     base.OnPreRender(e); 
     ScriptManager.RegisterStartupScript(this, GetType(), "test", "doSomething();", true); 
    } 

和JScript1.js文件看起來像下面。

function doSomething() { 
    alert('via dosomething control2 form'); 
} 

希望有所幫助。雖然我在實際場景中不得不亂拋垃圾,但這確實是我得到它的方式。

謝謝,