** 요점은 타입별로 사용자 정의 컨트롤(*.ascx)을 로드한다는데 있다.
우선 두가지의 ascx 를 생성한다.
Ex) TypeA.ascx , TypeB.ascx
*.aspx 는 다음과 같이 구성을 한다.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Controls_Default" %>
<!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>
<table>
<tr>
<td id="content" runat="server">
</td>
</tr>
</table>
<hr />
<div id="divContent" runat="server">
</div>
</div>
</form>
</body>
</html>
*.aspx.cs 는 다음과 같이 구성을 한다.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Controls_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control ctlA = new Control();
ctlA = this.Page.LoadControl("TypeA.ascx"); //ascx 로드
this.content.Controls.Add(ctlA);//테이블 삽입
Control ctlB = new Control();
ctlB = this.Page.LoadControl("TypeB.ascx");
this.divContent.Controls.Add(ctlB); //div 삽입
}
}
** 모든 컨트롤에 Add 가 되는 것은 아니었다. 일반적으로 컨테이너의 역활을 하는 컨트롤들(Table, div....)에만
동적으로 ascx 를 로드해서 삽입할수가 있다.