2012-10-30 30 views
0

我有一個nestedGridTemplate包含一個列表中的一個對象。如何加載模型並傳遞給ascx控件?

  this.Gridparent.MasterTableView.NestedViewTemplate = new Custom14NestedTemplate(this.model.Material.First()); 

這叫我的班public class Custom14Template

這個類調用:

public void InstantiateIn(System.Web.UI.Control container) 
    { 
     var myPage = new Page(); 
     Control c = myPage.LoadControl("~/Custom14/Templates/View.ascx"); 
     container.Controls.Add(c); 

     var x = new Label(); 
     x.Text = string.Format("qty : {0}.<br />", this.MyMaterial.Quantity); 
     container.Controls.Add(x); 
    } 

現在,我的ASCX只包含這個:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="View.ascx.cs" Inherits="*snip*.Templates.View" %> 

hello there 

一切都是正確顯示

 
    [grid start] 
    [item 1 : expanded] 
    hello there qty : 23. 
    [/item 1] 
    [item 2 /] 
    [item 3 /] 
    [/grid] 

我想通過我的對象到我的ASCX,從那裏建立我的HTML顯示使用... 相當於<%= html.EditorFor() %>(Asp.Net MVC)。而不是像我的標籤創建元素,並將它們添加到容器(在C#中構建html感覺很痛苦)。這是可行的嗎?怎麼樣?

回答

0

我適應this answer我需要(無ICustomControl):

調用文件包含以下內容:

public void InstantiateIn(System.Web.UI.Control container) 
    { 

     Page myPage = new Page(); 
     Control userControl = myPage.LoadControl("~/Templates/Edit.ascx"); 

     //this is the important part 
     if (userControl is Edit) 
     { 
      Edit customControl = userControl as Edit; 
      customControl.MyObject= this.PersistedObject; 
     } 

     container.Controls.Add(userControl); 
    } 

的.ascx.cs還修改了一下:

public partial class Edit : System.Web.UI.UserControl 
{ 

    public Produit MyObject { get; set; } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     this.product.Text = MyObject.ProductName; 
     this.Production.Text = MyObject.Production.ToString(); 

    } 
} 

而ascx只包含兩個平原,常客asp:textbox(ID =產品和生產)。

相關問題