开发者

How to deal with Web Controls in a class library which have file dependancies

I could do with a little guidance if possible..

I'm building a class library to contain custom web controls. I've transformed many of the jQuery UI elements into .NET classes for dynamic use in pages. e.g.

    Dim Msg As New Dialog("Dialog Title", New LiteralControl("Dialog Content"))
    Msg.Width.Value = 500
    Msg.Height.Value = 300
    Me.Controls.Add(Msg )

The necessary scripts get inserted into the head during CreateChildControls, and any jQuery file references are added to the head e.g.

    RegisterScriptFile("~/Scripts/jquery-1.5.1.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.core.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.widget.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.mouse.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.draggable.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.position.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.resizable.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.dialog.js")

The WebControl base class handles inserting the references into the Page Head.

This is brilliant, however there's a problem...the file paths may differ between applications which consume the control library.

My choices that I can see are:

A) Embed the files as resources within the library itself....however the CSS styling would be non-customisable between the individual aplications, and any changes to CSS/JS would need a re-compile.

B) Define and use a standardised file heirarchy. Each application needing a folder call 'Scripts' with a fixed file heirarchy within, so the control knows where to reference the required files. The problem I can see here is that it might not always be possible to use this standardised heirarchy and could make using the library cumbersome.

C) Create a property for each control, for each file it requires. Again this would become cumbersome and a pain to use, because each instantiated control would have to have those properties set.

D) Make some kind of ResourceUrlLibrary Dictionary like class which the consuming app can populate, then give to each control as it's instantiated. However, this seems convoluted and could cause confusion for other developers.

If anyone has come across this problem and could spare me some guidance t开发者_StackOverflow中文版hat would be brilliant :)


If its a custom control, it has a reference to the Page class, which then has a method call ResolveClientUrl to generate a relative URL for you. So that can take care of that scenario. You may want to expose a ScriptsFolder property that allows you to store the path to the scripts rather than hard-coding it too.

If this is for your own custom project, standardizing on a folder is fine, but if you are creating a common library to be reused, requiring a specific folder isn't a good idea, and you can then use the ScriptsFolder property to remedy this, or store the folder path in the config. It's OK to standardize on the use of an application setting.

To confirm, I use Telerik controls, and they go the route of having a property that defines a custom path to the script (since they rely on one for a specific control), and they also have certain settings that can be overridden by adding an application setting.

HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜