如何将模型加载到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

一切都正确显示

  [网格启动]  [项目1:扩展]     你好,数量:23。  [/项目1]  [项目2/]  [项目3/]  [/网格]

我想将我的对象传递到我的ASCX,以使用... old 等效的<%= html.EditorFor() %>(ASP.NET MVC)构建我的HTML显示。与其创建像我的标签这样的元素并将其添加到容器中(在C#中构建HTML感到很痛苦)。那可行吗?如何?

我对我的需求进行了调整(否 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);
    }

.cascx.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 =产品和生产)。

在http://asp.net-tutorials.com/user-controls/using/"动态加载"。易于传递可变信息或设置用户控制中的属性

中的属性

最新更新