0

我有一个nestedGridTemplate,其中包含一个列表对象。

            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,从那里使用...... (Asp.Net MVC)的等价物构建我的 html 显示。<%= html.EditorFor() %>而不是创建像我的标签这样的元素并将它们添加到容器中(在 c# 中构建 html 感觉很痛苦)。那可行吗?如何?

4

2 回答 2

0

我根据我的需要调整了这个答案(没有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= 产品和生产)。

于 2012-11-01T21:26:23.207 回答
0

在http://asp.net-tutorials.com/user-controls/using/ “动态加载”尝试解决方案。在用户控件中传递变量信息或设置属性的简单解决方案

于 2013-09-21T01:03:57.060 回答