面板web控件可讓開發人員將面板放在頁面上並定義將出現在面板內的內容。帶有可定義內容的用戶控件(如何創建模板化用戶控件)
是否可以通過用戶控件實現類似的功能,我可以在控件中定義所有自定義的「chrome」,但可以將它放在任何我想要的頁面上,並在每個實例中定義內容基礎?
面板web控件可讓開發人員將面板放在頁面上並定義將出現在面板內的內容。帶有可定義內容的用戶控件(如何創建模板化用戶控件)
是否可以通過用戶控件實現類似的功能,我可以在控件中定義所有自定義的「chrome」,但可以將它放在任何我想要的頁面上,並在每個實例中定義內容基礎?
我想你說的是Templated User Control。祝你好運!
他要求一個'UserControl',不是嗎? –
選項:
1)如通過烏韋和Hanlet稱爲
2)的jQuery UI庫
3)ASP.NET 2.0 Web部件
模板化控制這真的棘手的無論你走哪條路,都要執行。
正如HanletEscaño所指出的,這是完全可以做到的。我會告訴:
這是用戶控件標記:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TemplatedUserControl.ascx.cs" Inherits="Templated_User_Control.TemplatedUserControl" %>
<table border="1">
<tr>
<th>Head</th>
</tr>
<tr>
<td>
<asp:PlaceHolder ID="plContent" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td>Foot</td>
</tr>
</table>
這是用戶控制代碼:
using System;
using System.Web.UI;
namespace Templated_User_Control
{
public partial class TemplatedUserControl : System.Web.UI.UserControl
{
private ITemplate content_m = null;
[TemplateContainer(typeof(ContentContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Content
{
get
{
return content_m;
}
set
{
content_m = value;
}
}
void Page_Init()
{
if (content_m != null)
{
ContentContainer container = new ContentContainer();
//This is the real magic. Take the contents, and put it in our ContentContainer object, then
//add the ContentContainer object as a child control of the placeholder so it renders inside the user control
content_m.InstantiateIn(container);
this.plContent.Controls.Add(container);
}
}
protected void Page_Load(object sender, EventArgs e) {}
public class ContentContainer : Control, INamingContainer {}
}
}
這是頁面標記:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Templated_User_Control.Default" %>
<%@ Register src="TemplatedUserControl.ascx" tagname="TemplatedUserControl" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:TemplatedUserControl ID="TemplatedUserControl1" runat="server">
<Content>
Template user control test.<br />
<asp:Literal ID="litTest" runat="server">this is a test!</asp:Literal><br />
abc 123!
</Content>
</uc1:TemplatedUserControl>
</div>
</form>
</body>
</html>
唐'相信這是可能的'UserControl'派生的類。我認爲你必須實現['ITemplate'接口](http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.aspx)。 –
看起來有可能。 – Jeremy
好吧,我誤解了這個問題;我的鏈接似乎是正確的,雖然;-) –