2011-10-07 5 views
1

인라인 코드의 일부로 FormatStatus()라는 함수가 있고 실제 정의는 코드 숨김 (C#)에있는 아래에 붙여 넣은 작업 코드가 있습니다. 내 질문은 어떻게 내가 외부 자바 스크립트 파일에 FormatStatus() 함수를 이동하면 인라인 코드에서 호출 할 수 있습니다. 뒤에inlinecode에서 자바 스크립트 함수 호출 aspx

<asp:Label ID="lblSts" runat="server" Text= '<%# FormatStatus(Eval("StsId").ToString()) %>' >           
      </asp:Label> 

내 코드 : 단지 어떤 이벤트에서 호출 할 수있는 자바 스크립트 기능

protected string FormatStatus(string Id) 
    { 
     string formatText = string.Empty; 

     switch (int.Parse(Id)) 
     { 
      case 0: 
       formatText = "New"; 
       break; 
      case 1: 
       formatText = "Old"; 
       break; 
      ..... 
     } 

     return formatText; 
    } 

답변

1

. 초기 메서드로 js 함수를 실행하려면 window.onload를 사용할 수 있습니다. 그래서, 당신은 당신의 페이지에 글로벌 자바 스크립트 배열을 생성하고 코드 뒤에 C#을에서 아이디 값을 입력하고 window.load에 formatStatus를 호출 할 수 있습니다 : 다음

if (!Page.ClientScript.IsStartupScriptRegistered("preloadArray" + this.ClientID)) 
{ 
     string script = "<script type='text/javascript'> "; 
     for (int i = 0; i < ...; i++) 
     { 
      script += "arr.push("+i.ToString()+");"; 
     } 
     script += "formatStatus('" + gvAdminActiveAsgnments.ClientID + "');";    
     script += "</script>"; 

     Page.ClientScript.RegisterStartupScript(this.GetType(), 
      "preloadArray" + this.ClientID, script); 
} 

(당신이를 Page_Load 또는하여 ItemDataBound 핸들러를 사용할 수 있습니다)

당신은 그리드의 ID로 하나 개의 인수를 함수를 작성해야 :

function formatStatus(id){ 
var table = document.getElementById(id); 
var rows = table.getElementsByTag('TR'); 
for(var i=0; i<rows.length;i++){ 
    //puts into label from table row result for arr[i] 
} 
} 

그것은 ASP에서 렌더링됩니다 테이블, 페이지로드에 호출됩니다의 GridView. formatStatus를 모든 이벤트에 바인딩 할 수 있습니다 (예 : 어떤 버튼을 클릭하십시오. 따라서 레이블이 바뀝니다.

1

이렇게하면 문서 상단에 외부 js 파일을 포함시켜야합니다.

<asp:Label ID="lblSts" runat="server"> 
     <script type="text/javascript"> 
      document.write(FormatStatus('<%# Eval("StsId").ToString() %>'); 
     </script>          
</asp:Label> 
관련 문제