2010. 6. 18. 14:35
웹 컨트롤 툴킷을 사용하여 html 소스보기를 하게되면 자신이 사용하지 않은 자바스크립트가 포함되어 있는것을 확인 할 수 있다. 

 Custom Control을 개발할 때 그 컨트롤에 해당하는 자바스크립트 파일을 포함시키고자 할 때 사용한다. 

커스텀 컨트롤 클래스 선언
[ToolboxData(@"<{0}:CustomGridView runat=""server"" \>")]
public class CustomGridView : GridView
{

}


자바스크립트 파일 생성 및 구현

function MouseEnter(row,bgColor,textColor)
{
    
if (row.getAttribute("Selected"== "true"return;
    
row.style.backgroundColor bgColor;
    
row.style.color textColor;
}

function MouseLeave(row)
{
    
if (row.getAttribute("Selected"== "true"return;
    
row.style.backgroundColor row.getAttribute("OriginalColor");
    
row.style.color row.getAttribute("OriginalTextColor");
}

function MouseDown(row,bgColor,textColor)
{
    
if (row.getAttribute("Selected")=="true")
    { 
        row.style.backgroundColor 
row.getAttribute("OriginalColor");
        
row.style.color row.getAttribute("OriginalTextColor");
        
row.setAttribute("Selected","false");
    
}
    
else
    
{
        row.style.backgroundColor 
bgColor;
        
row.style.color textColor
        row.setAttribute(
"Selected","true");
    
}
}


자바스크립트 파일 리소스 등록

[assembly: WebResource("CustomControls.GridViewScript.js""text/javascript")]


클래스 내부의 자바스크립트 파일 리소스 사용

protected override void OnPreRender(EventArgs e)
{
    
base.OnPreRender(e);
    string 
resourceName "CustomControls.GridViewScript.js";

    
ClientScriptManager cs = this.Page.ClientScript;
    
cs.RegisterClientScriptResource(typeof(CustomControls.CustomGridView), resourceName);
}


실제 구현 페이지에서 선언된 컨트롤을 사용하면 아래와 같은 자바스크립트가 렌더링된다.

<script src="/ControlDemo/WebResource.axd?d=3SnAJwuH.......2&t=633134805717880000" type="text/javascript"></script>


출처 : 

http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx

Posted by CoolDragon