1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 using System.Text;
7 using System.Web.UI;
8
9 /// <summary>
10 /// 자바스크립트 클래스
11 /// </summary>
12 public class JavaScript : IDisposable
13 {
14 /// <summary>
15 /// 소멸자 호출 여부
16 /// </summary>
17 private bool IsDisposed;
18
19 private Page m_SourceDocument;
20
21 /// <summary>
22 /// 소스 코드 문자열 처리기
23 /// </summary>
24 private StringBuilder sb;
25
26 /// <summary>
27 /// 자바스크립트 소스 코드가 포함될 Page 객체
28 /// </summary>
29 public Page SourceDocument
30 {
31 get
32 {
33 return m_SourceDocument;
34 }
35 }
36
37 /// <summary>
38 /// 비동기 여부 (AJAX 페이지인 경우, 값은 true 입니다.)
39 /// </summary>
40 public bool IsAsynchronous
41 {
42 get
43 {
44 return m_SourceDocument.IsAsync;
45 }
46 }
47
48
49
50 /// <summary>
51 /// 기본 생성자
52 /// </summary>
53 /// <param name="TargetPage">자바스크립트 소스 코드가 포함될 대상 페이지</param>
54 public JavaScript(Page TargetPage)
55 {
56 sb = new StringBuilder();
57
58 m_SourceDocument = TargetPage;
59 }
60
61 /// <summary>
62 /// 기본 소멸자
63 /// </summary>
64 ~JavaScript()
65 {
66 if (IsDisposed == false)
67 {
68 Dispose(true);
69 }
70
71 Dispose(false);
72 }
73
74 /// <summary>
75 /// Dispose 소멸자
76 /// </summary>
77 /// <param name="isDisposing">Dispose 소멸 로직 실행 여부</param>
78 protected virtual void Dispose(bool isDisposing)
79 {
80 if (isDisposing == true)
81 {
82 // Dispose-time Code
83 if (sb != null)
84 {
85 sb = null;
86 }
87
88 if (m_SourceDocument != null)
89 {
90 m_SourceDocument.Dispose();
91 }
92
93 IsDisposed = true;
94 }
95 else
96 {
97 // Finalize-time Code
98 }
99 }
100
101 /// <summary>
102 /// Dispose 소멸자
103 /// </summary>
104 public void Dispose()
105 {
106 if (IsDisposed == false)
107 {
108 Dispose(true);
109 GC.SuppressFinalize(this);
110 }
111 }
112
113
114
115 /// <summary>
116 /// 자바스크립트 태그를 생성합니다.
117 /// </summary>
118 /// <param name="sourceCode">자바스크립트 소스 코드</param>
119 private void GenerateJavaScriptTag(string sourceCode)
120 {
121 if (sb.Length > 0)
122 {
123 sb.Remove(0, sb.Length);
124 }
125
126 sb.AppendLine("<script charset=\"UTF-8\" language=\"javascript\" type=\"text/javascript\">");
127 sb.AppendLine("<!--");
128 sb.AppendLine(@sourceCode);
129 sb.AppendLine("-->");
130 sb.AppendLine("</script>");
131 }
132
133
134
135 /// <summary>
136 /// 지정한 URL로 페이지를 이동합니다.
137 /// </summary>
138 /// <param name="TargetUri">이동할 URL 주소를 포함하는 Uri 객체</param>
139 public void RedirectToURL(Uri TargetUri)
140 {
141 GenerateJavaScriptTag("\tlocation.href = \"" + TargetUri.AbsoluteUri + "\";");
142
143 if (IsAsynchronous == false)
144 {
145 SourceDocument.ClientScript.RegisterStartupScript(SourceDocument.GetType(), "JSRedirectToURL", sb.ToString(), false);
146 }
147 else
148 {
149 ScriptManager.RegisterStartupScript(SourceDocument, SourceDocument.GetType(), "JSRedirectToURL", sb.ToString(), false);
150 }
151 }
152
153 /// <summary>
154 /// 지정한 URL로 페이지를 이동합니다.
155 /// </summary>
156 /// <param name="TargetURL">이동할 URL 주소 문자열</param>
157 public void RedirectToURL(string TargetURL)
158 {
159 GenerateJavaScriptTag("\tlocation.href = \"" + TargetURL + "\";");
160
161 if (IsAsynchronous == false)
162 {
163 SourceDocument.ClientScript.RegisterStartupScript(SourceDocument.GetType(), "JSRedirectToURL", sb.ToString(), false);
164 }
165 else
166 {
167 ScriptManager.RegisterStartupScript(SourceDocument, SourceDocument.GetType(), "JSRedirectToURL", sb.ToString(), false);
168 }
169 }
170
171 /// <summary>
172 /// 메시지 박스를 출력합니다..
173 /// </summary>
174 /// <param name="Message">메시지 내용</param>
175 public void ShowAlertMessageBox(string Message)
176 {
177 GenerateJavaScriptTag("\talert(\"" + @Message + "\");");
178
179 if (IsAsynchronous == false)
180 {
181 SourceDocument.ClientScript.RegisterClientScriptBlock(SourceDocument.GetType(), "JSAlertMessageBox", sb.ToString(), false);
182 }
183 else
184 {
185 ScriptManager.RegisterClientScriptBlock(SourceDocument, SourceDocument.GetType(), "JSAlertMessageBox", sb.ToString(), false);
186 }
187 }
188 }