ClientScriptManager.RegisterClientScriptBlock 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
클라이언트 스크립트를 개체에 등록합니다 Page .
오버로드
| Name | Description |
|---|---|
| RegisterClientScriptBlock(Type, String, String) |
형식, 키 및 스크립트 리터럴을 Page 사용하여 개체에 클라이언트 스크립트를 등록합니다. |
| RegisterClientScriptBlock(Type, String, String, Boolean) |
스크립트 태그 Page 를 추가할지 여부를 나타내는 형식, 키, 스크립트 리터럴 및 부울 값을 사용하여 클라이언트 스크립트를 개체에 등록합니다. |
RegisterClientScriptBlock(Type, String, String)
형식, 키 및 스크립트 리터럴을 Page 사용하여 개체에 클라이언트 스크립트를 등록합니다.
public:
void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script);
public void RegisterClientScriptBlock(Type type, string key, string script);
member this.RegisterClientScriptBlock : Type * string * string -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String)
매개 변수
- type
- Type
등록할 클라이언트 스크립트의 형식입니다.
- key
- String
등록할 클라이언트 스크립트의 키입니다.
- script
- String
등록할 클라이언트 스크립트 리터럴입니다.
예제
다음 코드 예제에서는 메서드의 사용을 보여 줍니다 RegisterClientScriptBlock .
<%@ 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 script on the page.
String csName = "ButtonClickScript";
Type csType = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> function DoClick() {");
csText.Append("Form1.Message.value='Text from client script.'; }");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
}
}
</script>
<html >
<head>
<title>RegisterClientScriptBlock 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">
Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
' Define the name and type of the client script on the page.
Dim csName 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 client script is already registered.
If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
Dim csText As New StringBuilder()
csText.Append("<script type=""text/javascript""> function DoClick() {")
csText.Append("Form1.Message.value='Text from client script.'; }")
csText.Append("</script>")
cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
End If
End Sub
</script>
<html >
<head>
<title>RegisterClientScriptBlock Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>
설명
클라이언트 스크립트는 키와 해당 형식으로 고유하게 식별됩니다. 키와 형식이 같은 스크립트는 중복된 것으로 간주됩니다. 지정된 형식과 키 쌍을 가진 하나의 스크립트만 페이지에 등록할 수 있습니다. 이미 등록된 스크립트를 등록하려고 시도해도 스크립트가 중복되지 않습니다.
메서드를 IsClientScriptBlockRegistered 호출하여 지정된 키와 형식 쌍이 있는 클라이언트 스크립트가 이미 등록되어 있는지 확인하고 불필요하게 스크립트를 추가하려고 시도하지 않도록 합니다.
메서드의 RegisterClientScriptBlock 이 오버로드에서는 매개 변수에 script 제공된 스크립트가 요소 블록에 <script> 래핑되어 있는지 확인해야 합니다.
메서드는 RegisterClientScriptBlock 렌더링된 페이지의 맨 위에 스크립트 블록을 추가합니다. 스크립트 블록은 등록된 순서대로 출력되도록 보장되지 않습니다. 스크립트 블록의 순서가 중요한 경우 개체를 StringBuilder 사용하여 스크립트를 단일 문자열로 함께 수집한 다음 모든 스크립트를 단일 클라이언트 스크립트 블록에 등록합니다.
추가 정보
적용 대상
RegisterClientScriptBlock(Type, String, String, Boolean)
스크립트 태그 Page 를 추가할지 여부를 나타내는 형식, 키, 스크립트 리터럴 및 부울 값을 사용하여 클라이언트 스크립트를 개체에 등록합니다.
public:
void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterClientScriptBlock(Type type, string key, string script, bool addScriptTags);
member this.RegisterClientScriptBlock : Type * string * string * bool -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String, addScriptTags As Boolean)
매개 변수
- type
- Type
등록할 클라이언트 스크립트의 형식입니다.
- key
- String
등록할 클라이언트 스크립트의 키입니다.
- script
- String
등록할 클라이언트 스크립트 리터럴입니다.
- addScriptTags
- Boolean
스크립트 태그를 추가할지 여부를 나타내는 부울 값입니다.
예외
클라이언트 스크립트 블록 유형은 .입니다 null.
예제
다음 코드 예제에서는 메서드의 사용을 보여 줍니다 RegisterClientScriptBlock . 매개 변수는 addScriptTags 시작 및 닫는 스크립트 태그가 매개 변수에 script 포함되지 않도록 설정 true 됩니다.
<%@ 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>
설명
클라이언트 스크립트는 키와 해당 형식으로 고유하게 식별됩니다. 키와 형식이 같은 스크립트는 중복된 것으로 간주됩니다. 지정된 형식과 키 쌍을 가진 하나의 스크립트만 페이지에 등록할 수 있습니다. 이미 등록된 스크립트를 등록하려고 시도해도 스크립트가 중복되지 않습니다.
메서드를 IsClientScriptBlockRegistered 호출하여 지정된 키와 형식 쌍이 있는 클라이언트 스크립트가 이미 등록되어 있는지 확인합니다. 이렇게 하면 스크립트를 불필요하게 추가하지 않도록 할 수 있습니다.
메서드의 RegisterClientScriptBlock 이 오버로드에서 매개 변수에 script 제공된 스크립트가 매개 변수를 사용하여 addScriptTags 요소 블록으로 <script> 래핑되는지 여부를 나타낼 수 있습니다.
true 스크립트 태그가 자동으로 추가됨을 나타내는 설정 addScriptTags 입니다.
메서드는 RegisterClientScriptBlock 렌더링된 페이지의 맨 위에 스크립트 블록을 추가합니다. 스크립트 블록은 등록된 순서대로 출력되도록 보장되지 않습니다. 스크립트 블록의 순서가 중요한 경우 개체를 StringBuilder 사용하여 스크립트를 단일 문자열로 함께 수집한 다음 모든 스크립트를 단일 클라이언트 스크립트 블록에 등록합니다.