2011-01-30 42 views
1

在usercontrol內部我有updatepanelanimationextender,當我將此控件添加到網頁我想將updatepanel的Id作爲參數傳遞給控件的屬性。在代碼隱藏設置updatepanelanimationextender TargetControId

用戶控件:

public partial class Controls_UpdateProgress : System.Web.UI.UserControl 
{ 
    public string UpdatePanelID { get; set; } 
    protected void Page_Load(object sender, EventArgs e) 
    {  
     if (!IsPostBack) 
     { 
      UpdatePanelAnimationExtender1.TargetControlID = UpdatePanelID;    
     } 

    } 
} 

<cc1:updatepanelanimationextender id="UpdatePanelAnimationExtender1" runat="server" > 
      <Animations> 
       <OnUpdating> 
        <Parallel duration="0" >   
         <ScriptAction Script="onUpdating();" /> 
        </Parallel> 
       </OnUpdating> 
       <OnUpdated> 
        <Parallel duration="0">    
        <ScriptAction Script="onUpdated();" /> 
        </Parallel> 
       </OnUpdated> 
      </Animations> 
     </cc1:updatepanelanimationextender> 

網頁:UpdatePanel1是在UpdatePanel的ID。

<uc1:UpdateProgress ID="UpdateProgress1" runat="server" UpdatePanelID="UpdatePanel1" /> 

我得到錯誤:

The TargetControlID of 'UpdatePanelAnimationExtender1' is not valid. The value cannot be null or empty.

+0

你有`啓用ViewState`?另外,什麼時候將某些內容分配給`UpdatePanelID`? – 2011-01-30 17:35:06

+0

viewstate已啓用。你的意思是什麼時候? 它的控制屬性 – urker 2011-01-30 17:36:38

回答

0

的AJAX控件擴展不存儲其TargetControlIDViewState(我在System.Web.Extensions 3.5與反射檢查,該酒店只獲取/設置私有成員)。因此,您存儲的值在回發時丟失。

你得值存儲在每一個請求:

protected void Page_Load(object sender, EventArgs e) 
{  
    UpdatePanelAnimationExtender1.TargetControlID = UpdatePanelID;    
} 
0

爲了避免這種情況的例外,你應該首先確保UpdatePanelID不爲空...

if (!IsPostBack)   { 
    if (UpdatePanelID != Null) { 
      UpdatePanelAnimationExtender1.TargetControlID = UpdatePanelID; 
    } 
} 

如果您希望設置的屬性以編程方式從父頁面更新UpdatePanelID,您將需要將UpdateProgress1強制轉換爲Controls_UpdateProgress實例。要做到這一點,做這樣的事情......

((Controls_UpdateProgress)UpdateProgress1).UpdatePanelID = "ThisIsTheIdYouWishToSet"; 
+0

UpdatePanelID在任何時候都不爲空,我希望設置從aspx而不是代碼 – urker 2011-01-30 17:47:46

相關問題