ClientScriptManager.RegisterStartupScript 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
시작 스크립트를 개체에 등록합니다 Page .
오버로드
| Name | Description |
|---|---|
| RegisterStartupScript(Type, String, String) |
형식, 키 및 스크립트 리터럴을 사용하여 시작 스크립트 Page 를 개체에 등록합니다. |
| RegisterStartupScript(Type, String, String, Boolean) |
스크립트 태그 Page 를 추가할지 여부를 나타내는 형식, 키, 스크립트 리터럴 및 부울 값을 사용하여 시작 스크립트를 개체에 등록합니다. |
RegisterStartupScript(Type, String, String)
형식, 키 및 스크립트 리터럴을 사용하여 시작 스크립트 Page 를 개체에 등록합니다.
public:
void RegisterStartupScript(Type ^ type, System::String ^ key, System::String ^ script);
public void RegisterStartupScript(Type type, string key, string script);
member this.RegisterStartupScript : Type * string * string -> unit
Public Sub RegisterStartupScript (type As Type, key As String, script As String)
매개 변수
- type
- Type
등록할 시작 스크립트의 형식입니다.
- key
- String
등록할 시작 스크립트의 키입니다.
- script
- String
등록할 시작 스크립트 리터럴입니다.
예제
다음 코드 예제에서는 메서드의 사용을 보여 줍니다 RegisterStartupScript . 시작 및 닫는 스크립트 태그는 매개 변수 내에 script 포함됩니다. 추가 매개 변수 설정에 따라 스크립트 태그를 추가하려면 메서드를 참조하세요 RegisterStartupScript .
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RegisterStartupScript</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
' Define the name and type of the client scripts on the page.
Dim csname1 As [String] = "PopupScript"
Dim cstype As Type = Me.[GetType]()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the startup script is already registered.
If Not cs.IsStartupScriptRegistered(cstype, csname1) Then
Dim cstext1 As New StringBuilder()
cstext1.Append("<script type=text/javascript> alert('Hello World!') </")
cstext1.Append("script>")
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString())
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>RegisterStartupScript</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
설명
클라이언트 스크립트는 키와 해당 형식으로 고유하게 식별됩니다. 키와 형식이 같은 스크립트는 중복된 것으로 간주됩니다. 지정된 형식과 키 쌍을 가진 하나의 스크립트만 페이지에 등록할 수 있습니다. 이미 등록된 스크립트를 등록하려고 시도해도 스크립트가 중복되지 않습니다.
메서드를 IsStartupScriptRegistered 호출하여 지정된 키 및 형식 쌍이 있는 시작 스크립트가 이미 등록되어 있는지 확인하고 스크립트를 불필요하게 추가하려고 시도하지 않도록 합니다.
메서드의 RegisterStartupScript 이 오버로드에서 매개 변수에 script 제공된 스크립트가 요소 블록으로 <script> 래핑되었는지 확인해야 합니다.
메서드에 RegisterStartupScript 의해 추가된 스크립트 블록은 페이지 로드가 완료된 후 페이지의 OnLoad 이벤트가 발생하기 전에 실행됩니다. 스크립트 블록은 등록된 순서대로 출력되도록 보장되지 않습니다. 스크립트 블록의 순서가 중요한 경우 개체를 StringBuilder 사용하여 스크립트를 단일 문자열로 함께 수집한 다음 모든 스크립트를 단일 클라이언트 스크립트 블록에 등록합니다.
추가 정보
적용 대상
RegisterStartupScript(Type, String, String, Boolean)
스크립트 태그 Page 를 추가할지 여부를 나타내는 형식, 키, 스크립트 리터럴 및 부울 값을 사용하여 시작 스크립트를 개체에 등록합니다.
public:
void RegisterStartupScript(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterStartupScript(Type type, string key, string script, bool addScriptTags);
member this.RegisterStartupScript : Type * string * string * bool -> unit
Public Sub RegisterStartupScript (type As Type, key As String, script As String, addScriptTags As Boolean)
매개 변수
- type
- Type
등록할 시작 스크립트의 형식입니다.
- key
- String
등록할 시작 스크립트의 키입니다.
- script
- String
등록할 시작 스크립트 리터럴입니다.
- addScriptTags
- Boolean
스크립트 태그를 추가할지 여부를 나타내는 부울 값입니다.
예외
type은 null입니다.
예제
다음 코드 예제에서는 메서드의 사용을 보여 줍니다 RegisterStartupScript . 매개 변수는 addScriptTags 시작 및 닫는 스크립트 태그가 매개 변수에 false 포함되도록 설정 script 됩니다.
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext1 = "alert('Hello World');";
cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the name and type of the client scripts on the page.
Dim csname1 As String = "PopupScript"
Dim csname2 As String = "ButtonClickScript"
Dim cstype As Type = Me.GetType()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the startup script is already registered.
If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
Dim cstext1 As String = "alert('Hello World');"
cs.RegisterStartupScript(cstype, csname1, cstext1, True)
End If
' Check to see if the client script is already registered.
If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then
Dim cstext2 As New StringBuilder()
cstext2.Append("<script type=""text/javascript""> function DoClick() {")
cstext2.Append("Form1.Message.value='Text from client script.'} </")
cstext2.Append("script>")
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>
설명
시작 스크립트는 키와 해당 형식으로 고유하게 식별됩니다. 키와 형식이 같은 스크립트는 중복된 것으로 간주됩니다. 지정된 형식과 키 쌍을 가진 하나의 스크립트만 페이지에 등록할 수 있습니다. 이미 등록된 스크립트를 등록하려고 시도해도 스크립트가 중복되지 않습니다.
메서드를 IsStartupScriptRegistered 호출하여 지정된 키 및 형식 쌍이 있는 시작 스크립트가 이미 등록되어 있는지 확인하고 스크립트를 불필요하게 추가하려고 시도하지 않도록 합니다.
메서드의 RegisterStartupScript 이 오버로드에서 매개 변수에 script 제공된 스크립트가 매개 변수를 사용하여 <script> 요소 블록으로 addScriptTags 래핑되는지 여부를 나타낼 수 있습니다.
addScriptTags 스크립트 태그가 자동으로 추가됨을 나타내는 설정 true 입니다.
메서드에 RegisterStartupScript 의해 추가된 스크립트 블록은 페이지 로드가 완료된 후 페이지의 OnLoad 이벤트가 발생하기 전에 실행됩니다. 스크립트 블록은 등록된 순서대로 출력되도록 보장되지 않습니다. 스크립트 블록의 순서가 중요한 경우 개체를 StringBuilder 사용하여 스크립트를 단일 문자열로 함께 수집한 다음 모든 스크립트를 단일 클라이언트 스크립트 블록에 등록합니다.