2009-11-22 8 views
0

IScriptControl을 구현하는 복합 컨트롤을 만들고 있습니다. 을 CreateChildControls에서javascript 함수에 액세스하는 복합 컨트롤

() 함수 난이있다 :

HtmlGenericControl ul = new HtmlGenericControl("ul"); 

HtmlGenericControl b2 = new HtmlGenericControl("li"); 
b2.Style["background-image"] = string.Format("url({0})", imageSrc);    
b2.Style["list-style"] = "none"; 
b2.Style["background-repeat"] = "no-repeat"; 
b2.Style["background-position"] = "center center";   
b2.Style["border-style"] = "none"; 
b2.Style["width"] = "20px"; 
b2.Style["height"] = "20px"; 
b2.Style["float"] = "left"; 
b2.InnerHtml = " "; 


b2.Attributes["onmouseover"] = 
b2.Attributes["onmouseout"] = 

ul.Controls.Add(b2);   
barContainer.Controls.Add(ul); 

제가 필요하면

b2.Attributes 설정하는 것 [ "onMouseover와"를]

b2.Attributes [ "onmous eout "]

Prototype Model에 정의 된 Javascript 기능의 속성.

ProjectW.Edition.prototype = { 
. 
. 
. 

MouseOver: function(ctrl) 
{ 
    DoWork... 
}, 


MouseOut: function(ctrl) 
{ 
    DoWork... 
}, 

이 필요 경우

#region IScriptControl Implementation 
     protected virtual IEnumerable<ScriptReference> GetScriptReferences() 
     {   

      ScriptReference reference = new ScriptReference(); 
      reference.Assembly = "ProjectW"; 
      reference.Name = "ProjectW.EditonScripts.js"; 

      return new ScriptReference[] { reference }; 

     } 


     protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors() 
     { 
      ScriptControlDescriptor descriptor = new ScriptControlDescriptor("ProjectW.Edition", this.ClientID); 


      descriptor.AddProperty(....);   

     );      

      return new ScriptDescriptor[] { descriptor };     
     } 

     IEnumerable<ScriptReference> IScriptControl.GetScriptReferences() 
     { 
      return GetScriptReferences(); 
     } 

     IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors() 
     { 
      return GetScriptDescriptors(); 
     } 

     #endregion 

UPDATE : 동적을 CreateChildControls 내부에서 발생하는 HTML 요소 - 런타임에.

답변

1

CompositeControl과 함께 HTMLControls을 사용하는 이유는 무엇입니까? 컨트롤이 이러한 간단한 태그로 만드는 경우. 따라서 대신 WebControl을 사용하십시오. 이런 것.

public override void RenderContents(HtmlTextWriter writer) 
{ 
    writer.RenderBeginTag(HtmlTextWriterTag.Ul); 

    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "..."); 
    . 
    . 
    . 
    writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_Foo"); 
    writer.RenderBeginTag(HtmlTextWriterTag.Li); 
    writer.Write("&nbsp;"); 
    writer.RenderEndTag(); 

    writer.RenderEndTag(); 

    base.RenderControl(writer); 
} 

ASP.NET Ajax 사용 컨트롤에 이벤트 처리기를 추가하면 몇 가지 간단한 차이가 있습니다. 대상 태그에 고유 한 ID를 추가해야합니다. 그리고 벨로우즈처럼 이벤트를 추가하십시오.

ProjectW.Edition.prototype = { 

    initialize: function() 
    { 
     base.callBaseMethod(this, "initialize"); 
     $addHandlers($get(this.get_id() + "_Foo"), "mouseover", Function.createDelegate(this, MouseOver)); 
    } 

    dispose: function() 
    { 
     base.callBaseMethod(this,"dispose"); 
     $removeHandler($get(this.get_id() + "_Foo"), "mouseover", Function.createDelegate(this, MouseOver)); 
    } 

    MouseOver: function(ctrl) 
    { 
     DoWork... 
    }, 

    MouseOut: function(ctrl) 
    { 
     DoWork... 
    } 

} 
+0

디자인 타임 지원이 필요하기 때문에 CompositeControl을 사용합니다. – markiz

+0

컨트롤에 많은 요소가 있습니다. 내 사례 시나리오를 보여주기 위해 하나의 섹션 만 붙여 넣었습니다. – markiz

+0

$ addHandlers in initialize 함수는 정적 일 수 없습니다. 왜냐하면 마크 업에서 생성 된 li 요소가 동적으로 생성되기 때문입니다. – markiz

관련 문제