use LoadTemplate, seehttp://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx
Public Class DataGridITemple1 Implements ITemplate Dim templatetype As ListItemType Dim columnName As String Sub New(ByVal type As ListItemType, ByVal colName As String) templatetype = type columnName = colName End Sub Overridable Overloads Sub InstantiateIn(ByVal container As Control) Dim lc As New Literal() Select Case templatetype Case ListItemType.Header lc.Text = "<b>" & columnName & "<b>" container.Controls.Add(lc) Case ListItemType.Item lc.Text = "Item" & columnName container.Controls.Add(lc) Case ListItemType.EditItem Dim tb As New TextBox() tb.Text = "" container.Controls.Add(tb) Case ListItemType.Footer lc.Text = "<I>Footer<I>" container.Controls.Add(lc) End Select End SubEnd Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingwebservercontroltemplatesprogrammatically.aspCreating Web Server Control Templates Programmatically
似乎那个样例只能实现普通得html,无法像硬编码的模板那样,嵌入aspx控件,有什么解决方法吗?
DynamicTemplate.TemplateString = "<tr><asp:Button id="Button1" runat="server" Text="0292297"></asp:Button></tr>"
DataGrid1.ItemTemplate = DynamicTemplate;
Dim columnName As String Sub New(ByVal type As ListItemType, ByVal colName As String) templatetype = type
columnName = colName End Sub Overridable Overloads Sub InstantiateIn(ByVal container As Control) Dim lc As New Literal()
Select Case templatetype
Case ListItemType.Header
lc.Text = "<b>" & columnName & "<b>"
container.Controls.Add(lc)
Case ListItemType.Item
lc.Text = "Item" & columnName
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I>Footer<I>"
container.Controls.Add(lc)
End Select End SubEnd Class
<ItemTemplate>
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<tr>
<td width="17.5%" height="63" rowspan="2" class="tableborderright"> <strong>用户:</strong>
<%# DataBinder.Eval(Container, "DataItem.yhzh") %>
</td>
<td width="83%" class="tableborderbottom"><img src="images/button1.gif" width="13" height="16" hspace="10"><strong>回复主题:</strong>
<%# DataBinder.Eval(Container, "DataItem.tzbt") %>
</td>
</tr>
<tr>
<td height="70" class="tableborderbottom">
<%# DataBinder.Eval(Container, "DataItem.tznr") %>
</td>
</tr>
<tr>
<td width="17.5%" height="47" class="textborder">
<img src="images/iconlogin.gif" width="26" height="27" align="absmiddle">
<br>
<%# DataBinder.Eval(Container, "DataItem.tzsj") %>
</td>
<td align="right"><a href="#_1"><strong>回复</strong></a><strong> <a href="consultation.aspx?reid=<%# DataBinder.Eval(Container, "DataItem.tzbh") %>&conid=<%=strconid%>">修改</a>
<a href="deleteconsult.aspx?reid=<%# DataBinder.Eval(Container, "DataItem.tzbh") %>" >删除</a></strong> </td>
</tr>
</table>
</ItemTemplate>