是否可以将对象传递给前端的usercontrol?

时间:2012-01-13 18:56:19

标签: asp.net webforms user-controls

无论如何通过前端标签将对象传递给usercontrol?我尝试过以下但是没有用。

后端

   public Range Range { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Popular channel range
        Range Range = new Range() 
        { 
            Min = 0, 
            Max = 8 
        };
    }

前端

<uc:PopularItems Range="<%=Range %>" runat="server" />

1 个答案:

答案 0 :(得分:12)

您不能将<%=与服务器控件一起使用。您应该使用<%#和数据绑定:

<强>后端

   [Bindable(true)]
   public Range Range { get; set; }

<强>前端

<uc:PopularItems ID="myControl" Range="<%# Range %>" runat="server" />

页面后端

   if(! IsPostBack) {
      myControl.DataBind();

      // or, to bind each control in the page:
      // this.DataBind();
   }
相关问题