2010-03-10 8 views
1

저는 학부생입니다. ASP.NET 서버 측 사용자 정의 컨트롤 안에 jQuery를 포함시키는 것과 관련된 몇 가지 쿼리가있었습니다.ASP.Net 서버 사용자 정의 컨트롤에 jQuery 임베드

개인 문자열 GetEmbeddedTextFile (문자열 sTextFile) { // 일반 문자열

 // we'll get the executing assembly, and derive 
     // the namespace using the first type in the assembly 
     Assembly a = Assembly.GetExecutingAssembly(); 
     String sNamespace = a.GetTypes()[0].Namespace; 

     // with the assembly and namespace, we'll get the 
     // embedded resource as a stream 
     Stream s = a.GetManifestResourceStream(
        string.Format("{0}.{1}",a.GetName().Name, sTextFile) 
       ); 

     // read the contents of the stream into a string 
     StreamReader sr = new StreamReader(s); 
     String sContents = sr.ReadToEnd(); 

     sr.Close(); 
     s.Close(); 

     return sContents; 
    } 
    private void RegisterJavascriptFromResource() 
    { 
     // load the embedded text file "javascript.txt" 
     // and register its contents as client-side script 
     string sScript = GetEmbeddedTextFile("JScript.txt"); 
     this.Page.RegisterClientScriptBlock("PleaseWaitButtonScript", sScript); 
    } 
    private void RegisterJQueryFromResource() 
    { 
     // load the embedded text file "javascript.txt" 
     // and register its contents as client-side script 
     string sScript = GetEmbeddedTextFile("jquery-1.4.1.min.txt"); 
     this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jQuery", sScript); 
     // this.Page.RegisterClientScriptBlock("JQueryResourceFile", sScript); 
    } 
    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 

     // the client-side javascript code is kept 
     // in an embedded resource; load the script 
     // and register it with the page. 
     RegisterJQueryFromResource(); 
     RegisterJavascriptFromResource(); 
    } 

같은 내용 // 포함 된 텍스트 파일 리소스를 검색하는 기능을하지만 난에 직면하고있는 문제가 별도의 .JS 파일로 작성한 내 사용자 정의 컨트롤에 포함하려고 시도한 모든 JQuery 코드가 화면에 출력으로 스트리밍됩니다. 또한 내 컨트롤이 제대로 동작하지 않아 jQuery 기능이 제대로 작동하지 않는 이유로 인해 작동하지 않습니다.

답변

1

자바 스크립트를 둘러싼 <script> 태그가 누락되었습니다. 그것이 사실 인 경우 스크립트 태그를 추가합니다 네 번째 부울 매개 변수를 RegisterClientScriptBlock의이 오버로드를 사용해보십시오.

Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jQuery", sScript, true); 
0

을 "나는에 쓴 내 모든 jQuery 코드 .JS 파일을 분리하고 내> 사용자 정의 컨트롤에 포함하려고 시도했습니다. 스트림입니다. 화면 출력 "으로 표시됩니다.

삽입 된 자바 스크립트를 다운로드하는 방법을 코드에 알려주는 코드를 추가해야합니다. 사용자 지정 컨트롤 프로젝트의 클래스 시작 부분 위에있는 어셈블리 주석으로 처리합니다. 형식은 다음과 같습니다.

<Assembly: System.Web.UI.WebResource("Namespace.ScriptName.js", "application/x-javascript")> 
관련 문제