views:

291

answers:

1

I am creating a composite control, that implements IScriptControl.

in CreateChildControls() function i have this:

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);

What I need is to set

b2.Attributes["onmouseover"]

and

b2.Attributes["onmouseout"]

attributes for Javascript functions that are defined in Prototype Model.

ProjectW.Edition.prototype = {
.
.
.

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


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

If this is needed:

  #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: The html elements that generated inside CreateChildControls dynamically - on runtime.

+1  A: 

Why you are using simple HTMLControls in combination with CompositeControl? If the control makes from these simple tags. Thus, use WebControl instead. Somthing like this.

 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);
 }

Adding event handler in ASP.NET Ajax Enabled controls has some simple differences. You should add a unique id to the target tag. And add the event to that like bellow.

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...
    }

}
Mehdi Golchin
I use CompositeControl because I need design time support.
markiz
The control has a lot of elements i've pasted only one section to show my case scenario
markiz
$addHandlers inside initialize function can't be static. Because the the li elements created,on the markup, dynamically.
markiz
WebControl supports design-time as well. If you want to assign a theme to the control and show it in the design-time so mark your control with `SupportsPreviewControlAttribute`. But, if you have partnership of multiple server side controls, so you have to derive the control from `CompositeControl`. Anyway, the above code is just an instance of what you have to do. I don't say your code is static. As demonstrated in my code snippet you have to add an auto generated and unique id to each of `li` element. Afterward, add the `onmouseover` and `onmousemove` event onto the initialize method to each.
Mehdi Golchin
For instance add an index to the end of control ClientID ('{ClientID}_1') and make a unique id for each of `li` element. And in the client-side, find the `li` elements are exist in the control element and use $addHandler to add specified events.
Mehdi Golchin
i think the "li finding" will complex the performance, is there more elegant way to do so?
markiz
And, is there reason why i should use WebControl and not CompositeControl?
markiz
You can use combination of JQuery and ASP.NET Ajax to find the li elements. for example `$(this.get_element()).children("li").click(Function.createDele...`. Also, you don't need any id for li elements in this way. Anyway, finding li elements doesn't have any pressure on the client. I don't know what does your control? So, I can't offer to you which of them (CompositeControl/WebControl) is better to use.
Mehdi Golchin
As I said, if your control is combination of another server control for example a TextBox and a Button control you have to derive your control from CompositeControl as it conceived from its name. But, if you want to render some simple HTML tags, so use WebControl and RenderContents instead of creating an instance for each HTMLcontrol and add it to the parent control.
Mehdi Golchin
I am creating text editor, that will render simple html elements (iframe, divs,ul and li). it will have design-time support.
markiz
It's a complex control but consists of simple html tags. you can use WebControl and RenderContents in this case.
Mehdi Golchin
Writing an HTML Editor spends many times to implement. Why you don't use the existing controls like TinyMCE and FreeTextBox? even if you want to create your own, use another Javascript editor controls cause their bugs are fixed on different browsers. I have implemented a TinyMCE editor and validator for ASP.NET which you can find it here http://tinymce.codeplex.com/
Mehdi Golchin
I need specific control for special tasks and none of the free controls do what i need. Anyway I accept your answer.
markiz