仅使用 HTML 和 Jquery 创建自定义控件

发布于 2024-10-30 18:53:32 字数 125 浏览 1 评论 0原文

我有一个仅使用 HTML 和 Jquery 创建的树视图。

我想知道我是否可以仅使用 HTML 和 jquery 创建一个自定义控件以使其可重复使用。如果是,那么有人可以告诉我如何实现这一目标,

提前致谢

I have a tree view created using HTML and Jquery only.

I would like to know can i create a custom control using just HTML and jquery to make it re-usable. If yes then can someone please throw some light on how i can achieve this

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难愈 2024-11-06 18:53:32

为了使其可作为 ASP.NET 控件重用,如果您想确保它可以安全地放置在任何地方,或者在一个页面上多次使用,它还必须命名容器安全。这意味着您需要确保向脚本传递对控件标记根的唯一 ID 的引用(例如 ClientID),并且任何其他引用都与此相关。

但一般来说,您可以通过多种方式使自定义控件成为独立的。如果它没有控件,则您可以将所有资源(HTML 和脚本)包含为嵌入资源 。控件可以在最简单的级别上通过 Render 方法将其自己的嵌入资源流式传输到客户端。

要处理脚本,您可以类似地使用 ScriptManager 将其自己的嵌入式 javascript 注册为客户端脚本块。但这并不理想,因为您的脚本将始终包含在块中,因此客户端可能无法很好地缓存。

更好的方法是使用 WebResource.axd 提供嵌入式资源。 这里有一篇文章解释了如何执行此操作。

就我个人而言,我发现 WebResource 和嵌入式资源是一个巨大的难题。您需要使 AssemblyInfo.cs 与资源、程序集名称和名称保持同步。命名空间约定似乎总是难以正确实现。我发现简单地公开自定义控件的静态方法要容易得多,例如:

public static Stream GetEmbeddedResource(someResourceIdentifier, out string mimeType) {
  /// return a stream of my own embedded resource ... can be used for anything
  /// css, js, html
}

然后只需创建自己的 Web 处理程序以用作资源加载器并传输内容。这要容易得多,而且您最终也不会得到 WebResource.axd 包含 200 个字符长的查询字符串。

In order to make it reusable as an ASP.NET control, it also has to be naming container safe, if you want to be sure it can be safely dropped anywhere, or used more than once on a page. This means you need to be sure that you are passing the script a reference to a unique ID of the root of the control's markup (e.g. the ClientID), and any other references are relative to that.

But generally, you can make a custom control self-contained in several ways. If it has no controls, then you could include all the resources (HTML and script) as embedded resources. The control can at the simplest level stream its own embedded resources to the client in the Render method.

To deal with scripts, you can similarly use ScriptManager to register it's own embedded javascript as a client script block. This is not ideal, though, because your script will always be included in a block, and therefore probably not cached very well by the clients.

A better way is to serve embedded resources using WebResource.axd. Here is an article that explains how to do this.

Personally, I find WebResource and embedded resources to be a huge pain in the neck. You need to keep AssemblyInfo.cs in sync with the resources, and assembly name & namespace conventions always seem to be impossibly difficult to get correct. I have found it much easier to simply expose a static method of the custom control, e.g.:

public static Stream GetEmbeddedResource(someResourceIdentifier, out string mimeType) {
  /// return a stream of my own embedded resource ... can be used for anything
  /// css, js, html
}

Then just create your own web handler to use as a resource loader and stream the contents. This is so much easier, and you also don't end up with WebResource.axd includes with 200 character long query strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文