■ PlaceHolder 클래스 : 동적 컨트롤 추가하기

------------------------------------------------------------------------------------------------------------------------

MainPage.aspx

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="TestProject.MainPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>PlaceHolder 클래스를 사용해 동적 컨트롤 추가하기</title>

    </head>

    <body>

        <form id="form" runat="server">

            <div>

                <asp:PlaceHolder ID="placeHolder" runat="server" />

                <hr />

                <asp:Label ID="messageLabel" runat="server" />

            </div>

        </form>

    </body>

</html>

 

 

MainPage.aspx.cs

 

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace TestProject

{

    /// <summary>

    /// 메인 페이지

    /// </summary>

    public partial class MainPage : Page

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 페이지 로드시 처리하기 - Page_Load(sender, e)

 

        /// <summary>

        /// 페이지 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        protected void Page_Load(object sender, EventArgs e)

        {

            Button button1 = new Button();

 

            button1.ID   = "button1";

            button1.Text = "버튼 1";

 

            button1.Click += button_Click;

 

            this.placeHolder.Controls.Add(button1);

 

            Literal literal = new Literal();

 

            literal.Text = "<br />";

 

            this.placeHolder.Controls.Add(literal);

 

            Button button2 = new Button();

 

            button2.ID   = "button2";

            button2.Text = "버튼 2";

 

            button2.Click += button_Click;

 

            this.placeHolder.Controls.Add(button2);

        }

 

        #endregion

        #region 버튼 클릭시 처리하기 - button_Click(sender, e)

 

        /// <summary>

        /// 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void button_Click(object sender, EventArgs e)

        {

            Button button = sender as Button;

 

            this.messageLabel.Text = $"{button.Text} 버튼이 클릭되었습니다.<br />{button.ID} 버튼 입니다.<br />";

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요