Blog Content

    티스토리 뷰

    73.C# ASP.NET - Ajax를 이용한 시간 출력 3

    반응형
    Master 페이지에 Ajax 컨트롤을 적용하여 시간을 출력하는 예제입니다.

    MasterPage.master

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

    <head runat="server">

        <title></title>

        <asp:ContentPlaceHolder id="head" runat="server">

        </asp:ContentPlaceHolder>

    </head>

    <body>

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

        <div>

       

            <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

           

            <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                <ContentTemplate>

                    현재시간 :

                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

                    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">

                    </asp:Timer>

                </ContentTemplate>

            </asp:UpdatePanel>   

        <hr />

       

            <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">       

            </asp:ContentPlaceHolder>

       

        <hr />

        </div>

        </form>

    </body>

    </html>


    MasterPage.master.cs

    protected void Timer1_Tick(object sender, EventArgs e)

    {

       Label1.Text = DateTime.Now.ToLongTimeString();

    }


    Web Form을 추가 하실때 마스터 페이지에 붙여주기 위해서
    마스터 페이지 선택을 체크 하신 후에 추가해주세요

    FrmScriptManagerProxy.aspx

    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

    </asp:Content>

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

        <p>

            <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">

            </asp:ScriptManagerProxy>

        </p>


        <asp:UpdatePanel ID="UpdatePanel2" runat="server">

            <ContentTemplate>

                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

            </ContentTemplate>

        </asp:UpdatePanel>

    </asp:Content>


    FrmScriptManagerProxy.aspx.cs

    protected void Button1_Click(object sender, EventArgs e)

    {

       Label2.Text = DateTime.Now.ToLongTimeString();

    }


    결과화면

    [그림73-1]



    반응형

    Comments