Asp.Net MVC2中 說到用戶自定義控件,就一肚子火氣,折騰過我好多回,這回總算看了點所以然
什么都不說了,code附上
1 情況一:通過controller創建的用戶自定義控件
2
//
Controllers
:TestAscx
4
5 public ActionResult TestAscx()
6 {
7 Ascx ascx = new Ascx();
8 return PartialView(ascx);
9 }
10 // View :TestAscx.ascx
11
12 <%@ Control Language= " C# " Inherits= " System.Web.Mvc.ViewUserControl<Ascx> " %>
13 在里面可以填寫你想要寫的東東
14
15 // 調用控件
16 View: index.aspx
17
18 <% Html. RenderAction( " TestAscx "); %>
4
5 public ActionResult TestAscx()
6 {
7 Ascx ascx = new Ascx();
8 return PartialView(ascx);
9 }
10 // View :TestAscx.ascx
11
12 <%@ Control Language= " C# " Inherits= " System.Web.Mvc.ViewUserControl<Ascx> " %>
13 在里面可以填寫你想要寫的東東
14
15 // 調用控件
16 View: index.aspx
17
18 <% Html. RenderAction( " TestAscx "); %>
情況二:調用ASP.NET 模式的用戶自定義控件,eg:footer.ascx
Controller中不需要任何東東,只需要在需要調用的地方
<% Html. RenderPartial( " footer "); %>
<% Html. RenderPartial( " footer "); %>