Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass an object to usercontrol in front-end?

Is there anyway to pass an object to an usercontrol through the frontend tags? I have tried the following but it doesn't work.

Backend

   public Range Range { get; set; }

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

Frontend

<uc:PopularItems Range="<%=Range %>" runat="server" />
like image 772
chobo Avatar asked Jan 13 '12 18:01

chobo


People also ask

What is Usercontrol in asp net?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.

Can we use user control in MVC?

In ASP.Net you need to register one tagprefix and then use the user control by tagprefix registered but in ASP.Net MVC using simple Thml. RenderPartial we can use the user control.


1 Answers

You can't use <%= with a server control. You should use <%# and databind:

Backend

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

Frontend

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

Backend of the page

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

      // or, to bind each control in the page:
      // this.DataBind();
   }
like image 107
onof Avatar answered Nov 09 '22 23:11

onof