2011-10-05 41 views

回答

1

我將創建一個ProductInfo控制屬性來公開txtPrice的價值,就像這樣:

public decimal Price 
{ 
    get { return Decimal.Parse(txtPrice.Text); } 
} 

然後在其他用戶的控制,嘗試這樣的事情:

ProductInfo prod = Page.FindControl("OtherUserControl") as ProductInfo; 
if (prod != null) 
{ 
    decimal price = prod.Price; 
} 

遞歸方法

您可能需要使用遞歸找到ProductInfo控制,如果你做這樣的事情應該工作:

private Control FindControlRecursive(string controlID, Control parentCtrl) 
{ 
    foreach (Control ctrl in parentCtrl.Controls) 
    { 
     if (ctrl.ID == controlID) 
      return ctrl; 
     FindControlRecursive(controlID, ctrl); 
    } 
    return null; 
} 

使用FindControlRecursive

ProductInfo prod = FindControlRecursive("OtherUserControl", Page) as ProductInfo; 
if (prod != null) 
{ 
    decimal price = prod.Price; 
} 
相關問題