【问题标题】:composite control accessing javascript functions访问 javascript 函数的复合控件
【发布时间】:2009-11-22 12:56:34
【问题描述】:

我正在创建一个复合控件,它实现了 IScriptControl。

在 CreateChildControls() 函数中我有这个:

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["鼠标悬停"]

b2.Attributes["onmouseout"]

原型模型中定义的 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

更新: 在 CreateChildControls 中动态生成的 html 元素 - 在运行时。

【问题讨论】:

    标签: asp.net javascript ajax controls


    【解决方案1】:

    为什么您将简单的HTMLControlsCompositeControl 结合使用?如果控件由这些简单的标签组成。因此,请改用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 Enabled 控件中添加事件处理程序有一些简单的区别。您应该向目标标签添加一个唯一的 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...
        }
    
    }
    

    【讨论】:

    • 我使用 CompositeControl 是因为我需要设计时支持。
    • 控件有很多元素我只粘贴了一个部分来展示我的案例场景
    • $addHandlers 里面的初始化函数不能是静态的。因为在标记上动态创建的 li 元素。
    • WebControl 也支持设计时。如果您想为控件分配主题并在设计时显示它,请使用SupportsPreviewControlAttribute 标记您的控件。但是,如果您有多个服务器端控件的伙伴关系,那么您必须从CompositeControl 派生控件。无论如何,上面的代码只是你必须做的一个实例。我不是说你的代码是静态的。如我的代码 sn-p 所示,您必须为每个 li 元素添加一个自动生成的唯一 ID。然后,将onmouseoveronmousemove 事件添加到每个的初始化方法中。
    • 例如,在控件 ClientID ('{ClientID}_1') 的末尾添加一个索引,并为每个 li 元素创建一个唯一的 id。而在客户端,找到控制元素中存在li元素,使用$addHandler添加指定事件。
    猜你喜欢
    • 1970-01-01
    • 2012-04-24
    • 2013-06-07
    • 2015-03-06
    • 1970-01-01
    • 2013-07-17
    • 2010-11-19
    • 1970-01-01
    • 2012-02-04
    相关资源
    最近更新 更多