TemplateControl.LoadTemplate(String) 方法

定义

从外部文件获取 ITemplate 接口的实例。Obtains an instance of the ITemplate interface from an external file.

public:
 System::Web::UI::ITemplate ^ LoadTemplate(System::String ^ virtualPath);
public System.Web.UI.ITemplate LoadTemplate (string virtualPath);
member this.LoadTemplate : string -> System.Web.UI.ITemplate
Public Function LoadTemplate (virtualPath As String) As ITemplate

参数

virtualPath
String

用户控件文件的虚拟路径。The virtual path to a user control file.

返回

ITemplate

指定模板的实例。An instance of the specified template.

示例

下面的代码示例演示如何使用 LoadTemplate 方法将用户控件作为替换项添加到 DataList 名为的 Web 服务器控件 DataList1The following code example demonstrates how to use the LoadTemplate method to add a user control as an alternating item to a DataList Web server control named DataList1. 用户控件实现 ITemplate 接口,并包含在名为 Newtemplate 的文件中。The user control implements the ITemplate interface and is contained in a file named Newtemplate.ascx.

void Page_Load(Object sender, EventArgs e) 
{
   if (!IsPostBack) 
   {  
      DataList1.AlternatingItemTemplate = LoadTemplate("newtemplate.ascx");
      DataList1.DataSource = CreateDataSource();
      DataList1.DataBind();
   }
}
Sub Page_Load(sender As [Object], e As EventArgs)
   If Not IsPostBack Then
      DataList1.AlternatingItemTemplate = LoadTemplate("newtemplate.ascx")
      
      DataList1.DataSource = CreateDataSource()
      DataList1.DataBind()
   End If
End Sub 'Page_Load

适用于

另请参阅