ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • aspx.cs 파일에 작성된 함수에서 aspx 파일에 있는 스크립트 접근 하는 방법
    예전 글들/.NET, C# 2011. 1. 11. 13:53
    반응형

    예제 소스
    -  aspx 파일
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ewwwe._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>
        <script type="text/javascript">
            function test(x, y) {
                var text1 = document.getElementById("text1");
                for (var i = 0; i < x.length; i++) {
                    text1.innerText += x[i];
                }
                text1.innerText += "\ny: " + y;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"
                style="height: 21px" />
        </div>
        <div id="text1">
        </div>
        </form>
    </body>
    </html>

    - aspx.cs 파일
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;

    namespace ewwwe
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                int[] x = new int[] { 1, 2, 3, 4, 5 };
                int[] y = new int[] { 1, 2, 3, 4, 5 };

                string xStr = getArrayString(x);
                string yStr = getArrayString(y);

                string script = String.Format("test({0}, {1})", xStr, yStr);
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "testFunction", script, true);
            }

            private string getArrayString(int[] array)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < array.Length; i++)
                {
                    sb.Append(array[i] + ",");

                }
                string arrayStr = string.Format("[{0}]", sb.ToString().TrimEnd(','));
                return arrayStr;
            }
        }
    }

    (소스 출처: http://stackoverflow.com/questions/788330/calling-a-javascript-function-from-an-aspx-cs-code-behind)

    그냥 내 생각정도?
     string script = String.Format("test({0}, {1})", xStr, yStr);
     this.Page.ClientScript.RegisterStartupScript(this.GetType(), "testFunction", script, true);
    아무래도 이게 가장 중요할 듯. 
    1) test({0}, {1}) 이런 식으로 aspx에 있는 스크립트 함수 접근하고,
    2) RegisterStartupScript(this.GetType(), "testFunction", script, true)
    - RegisterStartupScript(string key, string script)
     * key: 스크립트 블록을 식별하는 고유 키
     * script: 클라이언트에 보낼 스크립트의 내용
    - 클라이언트측 스크립트 블록 페이지 응답으로 내보냅니다.
    - Page 개체에 있는 <form runat = server> 요소이 닫는 태그 바로 앞에 스크립트를 내보냅니다. script매개 변수에 지정된 스크립트 블록 문자열은 열고 닥는 <script>요소로 묶어야 합니다.
     이 메서드는 키를 사용하여 스크립트 블록을 식별하므로 다른 서버 컨트롤 인스턴스에서 요청할 때마다 스크립트 블록을 출력 스트림으로 내보내지 않아도 됩니다.
     같은 key매개 변수가 있는 스크립트 블록은 중복된 것으로 간주됩니다. (출처: http://msdn.microsoft.com/ko-kr/library/system.web.ui.page.registerstartupscript(v=vs.80).aspx)

    반응형

    댓글

Designed by Tistory.