Monday, January 7, 2013

How to use UserControl in ASP.NET MVC?

1. Create UserControl as same as in the webforms. But change base class from System.Web.UI.UserControl to System.Web.Mvc.ViewUserControl
    public partial class TestUserControl : System.Web.Mvc.ViewUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Text = "Loaded";
        }
    }
2. Put this UserControl all related files into Views/Shared folder
3. Then able to load by Partial view
@Html.Partial("TestUserControl");

No comments:

Post a Comment